Multi-threading/Structs in C assignment

  • Thread starter frankfjf
  • Start date
  • Tags
    Assignment
In summary: I'll go back and fix that now. In summary, the code is failing to properly reference the created ringbuf_t structure, resulting in a different value being outputted from the main function.
  • #1
frankfjf
168
0
Hi all,

Although my homework assignment centers around practicing multi-threading type stuff (by implementing a ring buffer), my question is more centered on trying to figure something out that has nothing to do with multi-threading but nevertheless has me stumped on the assignment itself.

Okay, so, here's my header file's code for the ring buffer.

Code:
#include <pthread.h>

struct ringbuf_t
{
  pthread_mutex_t mutex;
  pthread_cond_t cond_full;
  pthread_cond_t cond_empty;
  int bufsiz;
  int front;
  int back;
  char* buf;
};

extern struct ringbuf_t* rb_init (int bufsiz);

extern void rb_finalize (struct ringbuf_t* rb);

extern int rb_size (struct ringbuf_t* rb);

extern int rb_is_full (struct ringbuf_t* rb);

extern int rb_is_empty (struct ringbuf_t* rb);

extern void rb_insert (struct ringbuf_t* rb, int c);

extern int rb_remove (struct ringbuf_t* rb);

And here's my first source file, which is intended to implement the above functions.

Code:
#include "ringbuf.h"
#include <stdio.h>
#include <pthread.h>

struct ringbuf_t* rb_init(int bufsiz)
{
  char temp[bufsiz];

  struct ringbuf_t newrb = { PTHREAD_MUTEX_INITIALIZER,
			     PTHREAD_COND_INITIALIZER,
			     PTHREAD_COND_INITIALIZER,
			     bufsiz, 0, bufsiz - 1, temp };

  struct ringbuf_t *p;

  return p;
}


Alright, so, as you can see, I've only given a shot at implementing the first method, which is just supposed to initialize the ring buffer and return a pointer to the new structure. Below is the contents of my main source file, which is supposed to tie it all together.

Code:
#include <stdio.h>
#include "ringbuf.h"

int main()
{
  printf("\nTesting first method...");
  struct ringbuf_t *p = rb_init(10);
  printf("\n%i", p->bufsiz);
  return 0;
}

Obviously, it's just barely interacting with the previous stuff. My problem is that I'm not getting the right values when testing out referencing individual members of the created ringbuf_t structure. The expected result of the main function is to spit back 10, the test size for the new ring buffer. But the main function isn't outputting 10, but instead outputs 108681. Why is this happening, and how do I fix my code to have it return the correct amount? Note that when I put a printf statement in the rb_init method itself to directly reference bufsiz, it gets the correct number, but not when referenced via the created pointer.

Thank you in advance.
 
Physics news on Phys.org
  • #2
Your rb_init function returns an uninitialized pointer and you allocate your array on the stack instead of on the heap. You absolutely need to read up on and understand how to do dynamic memory allocation and pointer management in C.
 
  • #3
Thanks for your help.

Oh wow, I had overlooked forgetting to fix the pointer return. Thank you for pointing that out especially!
 

Related to Multi-threading/Structs in C assignment

1. What is multi-threading and how does it work in C?

Multi-threading is a concept in computer science where a program can have multiple threads of execution running concurrently. In C, this is achieved by using the pthreads library, which allows for the creation and management of threads within a program. Each thread has its own stack and shares the same heap, allowing for efficient use of resources.

2. What are the benefits of using multi-threading in C?

Multi-threading in C can improve the performance of a program by allowing tasks to be executed in parallel. This can lead to faster execution times and better use of system resources. Additionally, multi-threading can improve the responsiveness of a program by allowing certain tasks to be performed in the background while others are being executed.

3. How are threads created and managed in C?

In order to create and manage threads in C, the pthreads library must be used. This library provides functions for creating, joining, and detaching threads, as well as functions for thread synchronization and communication. Threads can be created by calling the pthread_create() function and passing in a function pointer to the desired thread function.

4. What are structs in C and how are they used?

Structs in C are user-defined data types that allow for the grouping of related data. They are similar to classes in object-oriented programming languages, but do not support methods or inheritance. Structs are commonly used to represent complex data structures, such as a person with multiple attributes like name, age, and address.

5. How can structs be used in multi-threading in C?

Structs can be used in multi-threading in C to pass data between threads. Since each thread has its own stack, passing a struct by value will create a copy of the struct for each thread. This can be useful for sharing data in a thread-safe manner. Structs can also be used to store thread-specific data by using the pthread_setspecific() and pthread_getspecific() functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
969
Back
Top