Home > pthread mutex lock bus > pthread_mutex_lock bus error

Pthread_mutex_lock Bus Error

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Can't figure out Bus Error:10 when reading file up vote 0 down vote favorite I am making a program that reads a text file and puts it into a linked list before carrying on with the other functions. The program also involves a thread function that operates every 5 seconds but the crash is happening at startup. Here is my code for the functions causing problems: #include #include #include #include #include //Strucutres #define NODE struct node struct node { int number; char name[20]; NODE *next; }; #define LIST struct list struct list { NODE *head; NODE *tail; }; #define PARAM struct param struct param { char filename[20]; LIST *listpar; }; pthread_mutex_t mutex; //Declare functions void read_file(LIST **, char *in); LIST * add(LIST **list, char *fake, int fakenumber); void show(LIST **list); void delete(LIST **list); void dispbin(char *input, LIST **list); void *autosaver(void *arg); int main(int argc, char *argv[]) { LIST *list; FILE *in; PARAM *newP; printf("%s",argv[1]); printf("%s",argv[2]); pthread_t thr; pthread_mutex_init(&mutex, NULL); if ((list=(LIST *)malloc(sizeof(LIST)))==NULL) { printf("No memory available"); return 2; } list->head=NULL; list->tail=NULL; if (argc==1) { printf("No files entered"); return 1; } else if (argc==2) { printf("Text file entered but no binary file entered"); return 1; } else { printf("Reading file"); read_file(&list, argv[1]); } strcpy(newP->filename,argv[2]); newP->listpar=list; printf("About to create"); pthread_create(&thr, NULL, autosaver,(void *)newP); int whileLoop=0; int tempnumber; char tempName[20]; while (whileLoop==0) { newP->listpar=list; int selection; printf("\nPick an option:1)Add New Party 2)Enter Open Table Size 3)Show Text List 4)Show Binary Autosave 5)Quit: "); scanf("%d",&selection); NODE *new=list->head; switch(selection) { case 1: printf("

units of control that execute within the context of a single process representing multiple strands of indepenent execution. What is the difference between forking processes and threads? Well typically when a process is forked it executes as new independent process with its own PID and a copy of the code and resources of the parent process. It is scheduled by the http://stackoverflow.com/questions/27011607/cant-figure-out-bus-error10-when-reading-file OS as a independent process. A process has a single thread by default called main(). Threads running in a process get their own stack and run concurrently and share global variables in the process. We will need these skills for the robotics projects - we do a threaded design http://www.cs.dartmouth.edu/~campbell/cs50/threads.html for a project. In this lecture, we will just look at a number of simple examples of code to illustrate how threads are created and how we can implement mutual exclusion using mutex for shared resouces. These notes are not meant to be exhaustive - they are not. For a in depth look at pthreads read the following tutorial - it may help answer questions that you may have not covered in the class: POSIX Threads Programming, Blaise Barney, Lawrence Livermore National Laboratory Also, type "man pthread" for information on syntax, etc. Goals We plan to learn the following in today's lecture: Creating a thread Random behavior Safe and unsafe threads Mutex Deadlock issues Thread Creation The code creates a thread; that means two threads are running - the main thread and the print_i thread. C code: print_i.c // File print_i.c#include <stdio.h>#include <stdlib.h>#include <pthread.h>// global variable to share dataint i;// This function will run concurrently.void* print_i(void *ptr) {  while (1) {    sleep(1);    printf("%d\n", i);  }}int main() {  pthread_t t1;  i = 1;  int iret1 = pthread_create(&t1, NULL, print_i, NULL);  while (1) {    sleep(2);    i = i + 1;  }  exit(0); //never reached} Output from running the code $ gcc -o print_i print_i.c$ ./print_i122334455667788991010^Z[1]+  Stopped 

Programming Boards C++ Programming Yet another n00b in pthreads ... Getting started with C or C++ | C Tutorial http://cboard.cprogramming.com/cplusplus-programming/101027-yet-another-n00b-pthreads.html | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: Yet another n00b in pthreads ... Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-28-2008 #1 dimis View Profile View Forum Posts Registered User Join Date Mar pthread_mutex_lock bus 2008 Posts 16 Yet another n00b in pthreads ... Hi, I am new in pthread library and perhaps my question is naive. Anyway, here is the problem I want to solve. I want to create N >= 2 threads and one of them (from now on "generator") will always fill in with "chunks of data" a shared queue, while the rest pthread_mutex_lock bus error (N-1) threads will get the data from the queue (assuming there is something) - call these (N-1) "workers". For this purpose I decided to use a mutex for references in queue, as well as a condition variable. Hence, my idea is that, if the workers can't find data in queue, they should wait for a signal. Obviously the "generator" sends that signal once new data have been added into the queue. So, conceptually, here is how I implemented those functions initially (numbered lines for clarity): Code: 01: generator(){ 02: while (!created_required_amount_of_data) { 03: data <-- create_data () 04: lock (mutex) 05: enqueue (data) 06: if (there_are_threads_waiting) 07: send_condition_signal () // <---- NOT broadcast 08: unlock (mutex) 09: } 10: } I guess the above is fine. Let's switch to the problematic "worker": Code: 01: worker (){ 02: while (!processed_required_amount_of_data) { 03: lock (mutex) 04: if (queue_is_empty()) { 05: waiting_threads++ 06: wait_for_signal () 07: waiting_threads-- 08: } 09: data <-- dequeue_data () 10: unlock (mutex) 11: process (data) 12: } 13: } Unfortunately, the above scenario does NOT work a

 

Related content

No related pages.