?
Solved

POSIX, pthreads, semaphore API

Posted on 2003-02-20
3
Medium Priority
?
2,481 Views
Last Modified: 2013-11-15
I'd like to learn more about this topic, but I'm having a hard time finding sample code, and good online-tutorials.  If anyone knows of a few sites, could you please post them?

Thanks in advance!
0
Comment
Question by:CBMLude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 7991266
i found this tutorial really concise and helpful.. go to the IPC:semaphore section.. u can also look at other IPC methods

http://www.cs.cf.ac.uk/Dave/C/CE.html
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 120 total points
ID: 7991572
0
 

Author Comment

by:CBMLude
ID: 7992402
GREAT! I found
http://dis.cs.umass.edu/~wagner/threads_html/tutorial.html
to be very helpful.  Could I also get you or someone to explain what "Semaphore child_counter;" does?  I'm a little confused.  I think it creates child_counter of type Semaphore, but I don't know what the semaphore is initalized to...

void print_message_function( void *ptr );

Semaphore child_counter;
Semaphore worlds_turn;

main()
{
     pthread_t thread1, thread2;
     char *message1 = "Hello";
     char *message2 = "World";

     semaphore_init( &child_counter );
     semaphore_init( &worlds_turn );

     semaphore_down( &worlds_turn ); /* world goes second */
     
     semaphore_decrement( &child_counter ); /* value now 0 */
     semaphore_decrement( &child_counter ); /* value now -1 */
     /*
      * child_counter now must be up-ed 2 times for a thread blocked on it
      * to be released
      *
      */
     

     pthread_create( &thread1, pthread_attr_default,
                    (void *) &print_message_function, (void *) message1);

     semaphore_down( &worlds_turn );

     pthread_create(&thread2, pthread_attr_default,
                    (void *) &print_message_function, (void *) message2);

     semaphore_down( &child_counter );

     /* not really necessary to destroy since we are exiting anyway */
     semaphore_destroy ( &child_counter );
     semaphore_destroy ( &worlds_turn );
     exit(0);
}

void print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s ", message);
     fflush(stdout);
     semaphore_up( &worlds_turn );
     semaphore_up( &child_counter );
     pthread_exit(0);
}

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question