Semaphore Segmentation Fault


 
Thread Tools Search this Thread
Top Forums Programming Semaphore Segmentation Fault
# 1  
Old 09-28-2006
Semaphore Segmentation Fault

When I execute the first 4 lines of code , it works fine. But the output gives a segmentation fault on executing the sem_getvalue() function. I looke up everywhere for the syntax and other mistakes but I am not being able to find out whats wrong with the code. Can anyone please help me on that...??


char sem_fn[] = "my_sem";
sem_t * sem_des;
int* value;
sem_des = sem_open(sem_fn, O_CREAT | O_EXCL, 0666, 30);


sem_getvalue(sem_des,value);
# 2  
Old 09-28-2006
Is your sem_open call returning a valid semaphore value? Are you checking for that? For all you know the sem_open is returning SEM_FAILED.
# 3  
Old 09-28-2006
hi I even tried out using

x=sem_getvalue(sem_des,value);

and tried printing the value of x
but the execution does not reach there..It gives a segmentation fault immediately after the sem_open function.
# 4  
Old 09-28-2006
I also put in the check

if(sem_des == (void*)-1){
perror("sem_open failure");
printf("ERROR\n");
exit(1);
}

after sem_open() and printed
printf("%d\n",(int*)sem_des);

which prints the value as zero...but it fails as soon as it reaches the sem_getvalue()
# 5  
Old 09-28-2006
Notice that I didn't ask you to check the return value of the sem_getvalue call, but of sem_open. How about having your program printing that?

Something like this:
Code:
sem_des=sem_open(blah,blah);
fprintf(stdout,"sem_des: %d\n",sem_des);

Now does that seem too hard?
# 6  
Old 09-28-2006
I realised that you had asked for the return value of sem_open().Please read my next post after that in which I have mentioned that I got the return value as zero. I also got the return value as zero on using your snippet to print the value.
# 7  
Old 09-28-2006
Since it is returning a zero, I think sem_open() seems to be working fine. But then after that 'segmentation fault' occurs. Notice that this fault doesn't occur if I comment out the sem_getvalue() line from the code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

3. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

4. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

5. Programming

Segmentation fault in C

i have this code int already_there(char *client_names, char *username) { int i; for(i = 0; i<NUM; i++) { printf("HERE\n"); if (strcmp(client_names, username)==0) return(1); } return(0); } and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies

6. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

7. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

8. UNIX for Dummies Questions & Answers

Segmentation Fault

Hi, While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script. *** Error: The following error was encountered on the output file. Script.sh: 3043492 Segmentation fault(coredump) Please let me know how to get through it. ... (5 Replies)
Discussion started by: monika
5 Replies

9. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

10. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies
Login or Register to Ask a Question