Understanding a simple semaphore program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythoNewbie
    New Member
    • May 2007
    • 10

    Understanding a simple semaphore program

    Dear experts,

    I am trying to understand a simple semaphore program written in C, and trying to insert some printf statements in the code , but there is no output at all.


    #include<stdio. h>
    #include<phtrea d.h>
    #include<sys/ipc.h>
    #include<sys/sem.h>
    Code:
    int i, nb_place;
    int semid;
    struct sembuf operation;
     
    void reservation()
    {
    /*prinff*/
    /*operation p */
       operation.sem_num = 0;
    operation.sem_op = -1;
    operation.sem_flg = 0;
    semop(semid, &operation, 1);
    nb_place = nb_place -1;
     
    /*operation v */
    operation.sem_num = 0;
    operation.sem_op = 1;
    operation.sem_flg = 0;
    semop (semid, &operation, 1);
    }
     
    main()
    {
    /*prinf*/
      pthread_t num_thread[3];
    /*creation of a semaphore initialised to value 1 */
     
    semid = semget(12,1,IPC_CREAT|IPC_EXCL|0600);
    semctl(semid,0,SETVAL,1);
     
    for(i=0; i<3; i++)
    {
      /*printf*/
    pthread_create(&num_thread[i], NULL, (void *(*)())reservation, NULL);
    pthread_join(num_thread, NULL);
     
    semctl(semid,0,IPC_RMID,0);
    }
    }
    In the previous when I locate a printf statement in the /*printf*/ nothing is displayed, why is this? and i would be grateful if you give a small explanation of what is supposed to be happening in this program.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't see any printf statements in your code.

    /*printf*/ is a comment.

    Comment

    • pythoNewbie
      New Member
      • May 2007
      • 10

      #3
      sorry for not being obvious, the printf statements are flags but they are not displayed when i execute the program. the following code is more obvious

      Code:
      int i, nb_place;
      int semid;
      struct sembuf operation;
      void reservation()
      {
          printf("FLAG1");
          /*operation p */
          operation.sem_num = 0;
          operation.sem_op = -1;
          operation.sem_flg = 0;
          semop(semid, &operation, 1);
          nb_place = nb_place -1;
          /*operation v */
          operation.sem_num = 0;
          operation.sem_op = 1;
          operation.sem_flg = 0;
          semop (semid, &operation, 1);
      }
      
      main()
      {
          printf("FLAG 2");
          pthread_t num_thread[3];
          /*creation of a semaphore initialised to value 1 */
          semid = semget(12,1,IPC_CREAT|IPC_EXCL|0600);
          semctl(semid,0,SETVAL,1);
          for(i=0; i<3; i++)
          {
              printf("FLAG 3");
              pthread_create(&num_thread[i], NULL, (void *(*)())reservation, NULL);
              pthread_join(num_thread, NULL);
              semctl(semid,0,IPC_RMID,0);
          }
      }
      Last edited by Banfa; Feb 3 '09, 12:11 PM. Reason: Clearing up code

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Just to be sure print a newline character at the end of your output strings; stdout may be line buffered so nothing will show up for a while. Do this instead: printf("FLAG 1\n");

        kind regards,

        Jos

        Comment

        • pythoNewbie
          New Member
          • May 2007
          • 10

          #5
          Thank you JosAH very much
          > so nothing will show up for a while
          well, actually nothing is showed up always. the newline character works fine.

          I have a question about the semget function, I want to understand it better, isn't there any SIMPLE tutorials for that?

          semid = semget(12,1,IPC _CREAT|IPC_EXCL |0600);

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by pythoNewbie
            I have a question about the semget function, I want to understand it better, isn't there any SIMPLE tutorials for that?

            semid = semget(12,1,IPC _CREAT|IPC_EXCL |0600);
            Unix programmers don't need tutorials; they read the manual pages.

            kind regards,

            Jos ;-)

            Comment

            • pythoNewbie
              New Member
              • May 2007
              • 10

              #7
              Aha

              I will try this one, thank you very much

              Comment

              Working...