Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <semaphore.h>
- #include <pthread.h>
- #include <wait.h>
- #include <unistd.h>
- sem_t pingsemafor,pongsemfor;
- void *ping(void *arg){
- while(1){
- sem_wait(&pingsemafor);
- printf("\n Ping");
- sleep(1);
- sem_post(&pongsemfor);
- }
- }
- void *pong(void *arg){
- while(1){
- sem_wait(&pongsemfor);
- printf("\n Pong");
- sleep(1);
- sem_post(&pingsemafor);
- }
- }
- int main()
- {
- sem_init(&pingsemafor,0,1);
- sem_init(&pongsemfor,0,0);
- pthread_t ping_thread,pong_thread;
- pthread_create(&ping_thread,NULL,ping,NULL);
- pthread_create(&pong_thread,NULL,ping,NULL);
- pthread_join(&ping_thread,NULL);
- pthread_join(&pong_thread,NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement