What is wrong with this function?

This is a discussion on What is wrong with this function? within the Linux Programming forums, part of the Platform Specific Boards category; Ok, i have this function and for some reason the return value is ........... Code: sem_t *semn_open(int id){ return (sem_t*)sem_open("/999", ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    6

    What is wrong with this function?

    Ok, i have this function and for some reason the return value is ...........

    Code:
    sem_t *semn_open(int id){
      return (sem_t*)sem_open("/999", O_CREAT);
    }
    Obviously this is a simplified version of the function... but it doesn't work anyway!

    I'm calling this with:

    Code:
    sem_t *temp;
    // causes segmentation fault
    temp = semn_open(999);
    // all OK
    // temp = sem_open("/999", O_CREAT);
    Can anyone help me?

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Yeah, too simplified to make any sense out of it.

    Post an actual example which fails, not random single lines which you think might be the problem.

    What is the pointless cast on
    return (sem_t*)sem_open("/999", O_CREAT);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    OK sorry!

    Here is a fully functional code. Surprisingly I noticed that the problem doesn't come from my_sem_open but from sem_open itself (?!)

    I don't get any errors but the output is "* test * test * test * test ...."

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <semaphore.h>
    
    sem_t *my_sem_open() {
       return sem_open("/100", O_CREAT);
    }
    
    int main() {
       sem_unlink("/100");
       sem_t *temp;
       if ((temp = sem_open("/100", O_CREAT)) == (void*)-1) {
          printf("error!!!\n");
          return 0;
       }
       printf("going to wait..\n");
       for (;;) {
          sem_wait(temp);
          printf("* test ");
       }
       return 0;
    }

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Heh - I get a segfault

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Apparently, you need to read the manual
    Quote Originally Posted by a random manual page
    O_CREAT This flag is used to create a semaphore if it does not
    already exist. The O_CREAT argument requires additional
    arguments:
    mode of type mode_t, and value of type unsigned
    int. After the semaphore is created, other processes can
    open the semaphore by calling sem_open with the same value
    for the name argument.
    If you don't supply the required arguments, it just uses whatever random junk is on the stack, and thus leads to chaotic behaviour depending on how you arrange the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    OOoops...

    Got it! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21