Home > pthread bus > pthread bus error 10

Pthread Bus Error 10

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 Getting bus error 10 with pthreads up vote 3 down vote favorite My command line tool keeps throwing the bus error: 10 message. Xcode debugger shows EXC_BAD_ACCESS message and highlights the function call that creates the thread. Manual debugging shows that the execution flow breaks at random positions inside the thread flow. I tried another compiler (gcc), but it ended up the same. Disabling pthread_mutex_lock() and pthread_mutex_unlock() doesn't help. I wrote this small example that reproduces the error. #include #include #include typedef struct thread_args { pthread_mutex_t* mutex; } thread_args; void* test(void* t_args) { printf("Thread initiated\n"); thread_args* args = (thread_args* )t_args; printf("Args casted\n"); pthread_mutex_lock(args->mutex); printf("Mutex locked\n"); pthread_mutex_unlock(args->mutex); printf("Mutex unlocked\n"); pthread_exit(NULL); } int main() { pthread_mutex_t mutex1; pthread_mutex_init(&mutex1, NULL); thread_args args; args.mutex = &mutex1; pthread_t* thread; printf("Initiating a thread\n"); pthread_create(thread, NULL, test, &args); return(0); } c multithreading debugging pthreads share|improve this question edited Jun 23 '15 at 19:57 Sourav Ghosh 71.1k1174130 asked Jun 23 '15 at 19:38 Eli Korvigo 3,4612730 add a comment| 3 Answers 3 active oldest votes up vote 2 down vote accepted I think, in your case, pthread_create(thread, NULL, test, &args); at this call, thread is a pointer and not allocated memory. So, essentially pthread_create() tries to write into uninitialized memory, which creates undefined behavior. Referring the man page of pthread_create() Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread;...

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 Bus error: 10 error up vote 23 down vote favorite 10 Here is my code #import http://stackoverflow.com/questions/31011959/getting-bus-error-10-with-pthreads #import int main(int argc, const char *argv[]) { char *str = "First string"; char *str2 = "Second string"; strcpy(str, str2); return 0; } It compiles just fine without any warning or errors, but when I run the app I get that error Bus error: 10 What I miss ? c share|improve this question asked Jan 3 '12 at 18:09 summerc 5312715 1 Well, strlen(str)http://stackoverflow.com/questions/8716714/bus-error-10-error 18:11 21 Is everyone missing the #import?!! –Sangeeth Saravanaraj Jan 3 '12 at 18:18 4 @SangeethSaravanaraj Yes, I can't believe it myself. lol Everyone missed it... –Mysticial Jan 3 '12 at 18:24 1 also const char *argv[] which is not valid in hosted environment. You should use char *argv[] –ouah Jan 3 '12 at 18:25 add a comment| 7 Answers 7 active oldest votes up vote 30 down vote accepted For one, you can't modify string literals. It's undefined behavior. To fix that you can make str a local array: char str[] = "First string"; Now, you will have a second problem, is that str isn't large enough to hold str2. So you will need to increase the length of it. Otherwise, you will overrun str - which is also undefined behavior. To get around this second problem, you either need to make str at least as long as str2. Or allocate it dynamically: char *str2 = "Second string"; char *str = malloc(strlen(str2) + 1); // Allocate memory // Maybe check for NULL. strcpy(str, str2); // Always remember to free it. free(str); There are other more elegant ways to do this involving VLAs (in C99) and stack allocation, but I won't go into those as their use is somewhat ques

a Question Ask for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get Help Ask a Question Ask for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get https://www.experts-exchange.com/questions/20994492/Bus-error-with-pthread.html Help Expand Search Submit Close Search Login Join Today Products BackProducts Gigs Live Courses http://www.cs.dartmouth.edu/~campbell/cs50/threads.html Vendor Services Groups Careers Store Headlines Website Testing Experts Exchange > Questions > Bus error with pthread Want to Advertise Here? Solved Bus error with pthread Posted on 2004-05-19 C 1 Verified Solution 4 Comments 802 Views Last Modified: 2011-09-20 Hi, This program takes files name as arguments and count lines on each by thread. in execution times pthread bus it gave me "Bus error" #include #include #include FILE *in; int count; int sum1=0,i; char Buffer[256]; CountLines (char *file){ strcpy(input, file); in = fopen(input, "r"); if (in == NULL) { printf("Error In Files opening\n"); exit(-1); } count = 0; while (1) { fgets (Buffer, 256, in); if (feof (in)) break; ++count; } /*sum1=sum1+count;*/ sum1=sum1+count; } int main (int argc, pthread bus error char *args[]) { pthread_t tid1; for (i=1;i

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 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 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; 

 

Related content

No related pages.