Results 1 to 5 of 5
Hi all,
I am executing a program. I have actually got this program from my friend.
This program is to study and solve the problem of Reader/Writer Problem.
But when ...
- 08-18-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 7
Segmentation fault in C program
Hi all,
I am executing a program. I have actually got this program from my friend.
This program is to study and solve the problem of Reader/Writer Problem.
But when i execute this program it gives me : /Segmentation error
-------------------------------------------------------------------------------------
rdwrt.c
-------------------------------------------------------------------------------------
/* program to study Reader/Writer problem with Semaphore */
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void reader1();
void writer1();
void reader2();
void writer2();
sem_t rsem;
sem_t wsem;
sem_t x,y,z;
int rc=0, wc=0;
int main()
{
int r,w,i=0,ch=2;
pthread_t t1[5],t2[5];
printf ("\n\nEnter the number of READERS: ");
scanf ("%d", &r);
printf ("\n\nEnter the number of WRITERS: ");
scanf ("%d", &w);
printf ("\n\nMENU\n\n\t1.READERS HAVE PRIORITY");
printf ("\n\n\t2.WRITER HAVE PRIORITY");
printf ("\n\n\tENTER YOUR CHOICE:");
scanf ("%d", &ch);
sem_init(&rsem, 0, 1);
sem_init(&wsem, 0, 1);
sem_init(&x, 0, 1);
sem_init(&y, 0, 1);
sem_init(&z, 0, 1);
if (ch==1)
{
pthread_create(&t1[0], NULL, (void *)reader1, 0);
pthread_create(&t2[0], NULL, (void *)writer1, 0);
for (i=1;i<r-1;i++)
pthread_create(&t1[i], NULL, (void *)reader1, (void *)i );
for (i=1; i<w-1; i++)
pthread_create(&t1[i], NULL, (void *)reader1, (void *)i );
for (i=1; i<w-1; i++)
pthread_create(t2[i], NULL, (void *)writer1, (void *)i);
pthread_create(&t1[w-1], NULL, (void *)reader1, (void *)(w-1) );
pthread_create(&t2[r-1], NULL, (void *)writer1, (void *)(r-1) );
for (i=0;i<r;i++)
pthread_join(t1[i], NULL);
for (i=0;i<w;i++)
pthread_join(t2[i], NULL);
}
else if (ch==2)
{
pthread_create(&t2[0], NULL, (void *)writer2, 0);
pthread_create(&t1[0], NULL, (void *)reader2, 0);
for (i=1;i,r-1;i++)
pthread_create(&t2[i], NULL, (void *)reader2, (void *)i );
for (i=1;i<w-1;i++)
pthread_create(&t1[i], NULL, (void *)writer2, (void *)i );
pthread_create (&t2[r-1], NULL, (void *)writer2, (void *)(r-1) );
pthread_create (&t1[w-1], NULL, (void *)reader2, (void *)(w-1) );
for(i=0; i<r; i++)
pthread_join(t2[i], NULL);
for(i=0; i<w; i++)
pthread_join(t1[i], NULL);
}
else
printf ("\nINVALID CHOICE....PROGRAM TERMINATED.");
sem_destroy(&rsem);
sem_destroy(&wsem);
return 0;
} // main function close
void reader1 (void *i)
{
sem_wait(&rsem);
rc++;
if (rc==1)
sem_wait(&wsem);
sem_post(&rsem);
printf ("\nREADER %d IS READING NOW", (int)i);
sleep(1);
sem_wait(&rsem);
rc--;
if(rc==0)
sem_post(&wsem);
sem_post(&rsem);
printf ("\nREADER %d HAS FINISHED READING");
}
void writer1(void *i)
{
sem_wait(&wsem);
printf ("\nWRITER %d IS WRITING NOW", (int) i);
sleep(1);
sem_post(&wsem);
printf ("\nWRITER %d HAS FINISHED WRITING");
}
void reader2(void *i)
{
sem_wait(&z);
sem_wait(&rsem);
sem_wait(&x);
rc++;
if(rc==1)
sem_wait(&wsem);
sem_post(&x);
sem_post(&rsem);
sem_post(&z);
printf ("\nREADER %d IS READING NOW", (int)i);
sleep(1);
sem_wait(&x);
rc--;
if (rc==0)
sem_post(&wsem);
sem_post(&x);
printf ("\nREADER %d HAS FINISHED READING");
}
void writer2(void *i)
{
sem_wait(&y);
wc++;
if (wc==1)
sem_wait(&rsem);
sem_post(&y);
sem_wait(&wsem);
printf ("\nWRITER %d IS WRITING NOW", (int) i);
sleep(1);
sem_post(&wsem);
sem_wait(&y);
wc--;
if(wc==0)
sem_post(&rsem);
sem_post(&y);
printf ("\nWRITER %d HAS FINISHED WRITING");
}
-----------------------------------------------------------------------------------------------------------------------------
*****************
O/P:
*****************
Enter the number of READERS: 4
Enter the number of WRITERS: 4
MENU
1.READERS HAVE PRIORITY
2.WRITER HAVE PRIORITY
ENTER YOUR CHOICE:1
READER 0 IS READING NOW
READER 1 IS READING NOW
READER 2 IS READING NOW
READER 1 IS READING NOW
Segmentation fault
*************************************************
Why i am getting this run time error?
Can you suggest me how can i correct my code?
- 08-18-2009 #2
your code is difficult to read, you should post it inside of code tags and add proper indentations, also this is a homework question, we are not allowed to directly answer them, the easiest way to find out the cause of the seg fault is to run it through a debugger like gdb and determine the line that is causing the segmentation fault
- 08-19-2009 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
My guess is that the semaphores are being destroyed in main() before the threads are finished using them.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-09-2009 #4Just Joined!
- Join Date
- Sep 2009
- Posts
- 1
hey.. im gettin da same error...
im new to linux c programming.. i wrote a program fr generation of prime numbers.. whose execution gives segmentation fault... dunno wat it means.. plz help!!
nd hw do u use gdb??
- 09-09-2009 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
To use gdb, you need to compile your program files with the -g option, then you can run them in gdb: gdb progname
In any case, read the man pages and gdb documentation for more information how to use it. If you run your program inside gdb, it will stop and show you where it segfaults. A segmentation fault (SIGSEGV) means that your program tried to access invalid memory. This can happen when you have uninitialized pointer variables (their value is indeterminate) and try to read/write from/to them. There are other causes as well, such as accessing a memory location after it has been freed. This happens a lot in C++ when you try to access an object by reference or pointer after the object itself has gone out of scope and has been destroyed.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
