473,490 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Understanding a simple semaphore program

10 New Member
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<phtread.h>
#include<sys/ipc.h>
#include<sys/sem.h>
Expand|Select|Wrap|Line Numbers
  1. int i, nb_place;
  2. int semid;
  3. struct sembuf operation;
  4.  
  5. void reservation()
  6. {
  7. /*prinff*/
  8. /*operation p */
  9.    operation.sem_num = 0;
  10. operation.sem_op = -1;
  11. operation.sem_flg = 0;
  12. semop(semid, &operation, 1);
  13. nb_place = nb_place -1;
  14.  
  15. /*operation v */
  16. operation.sem_num = 0;
  17. operation.sem_op = 1;
  18. operation.sem_flg = 0;
  19. semop (semid, &operation, 1);
  20. }
  21.  
  22. main()
  23. {
  24. /*prinf*/
  25.   pthread_t num_thread[3];
  26. /*creation of a semaphore initialised to value 1 */
  27.  
  28. semid = semget(12,1,IPC_CREAT|IPC_EXCL|0600);
  29. semctl(semid,0,SETVAL,1);
  30.  
  31. for(i=0; i<3; i++)
  32. {
  33.   /*printf*/
  34. pthread_create(&num_thread[i], NULL, (void *(*)())reservation, NULL);
  35. pthread_join(num_thread, NULL);
  36.  
  37. semctl(semid,0,IPC_RMID,0);
  38. }
  39. }
  40.  
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.
Feb 2 '09 #1
6 9336
weaknessforcats
9,208 Recognized Expert Moderator Expert
I don't see any printf statements in your code.

/*printf*/ is a comment.
Feb 2 '09 #2
pythoNewbie
10 New Member
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

Expand|Select|Wrap|Line Numbers
  1. int i, nb_place;
  2. int semid;
  3. struct sembuf operation;
  4. void reservation()
  5. {
  6.     printf("FLAG1");
  7.     /*operation p */
  8.     operation.sem_num = 0;
  9.     operation.sem_op = -1;
  10.     operation.sem_flg = 0;
  11.     semop(semid, &operation, 1);
  12.     nb_place = nb_place -1;
  13.     /*operation v */
  14.     operation.sem_num = 0;
  15.     operation.sem_op = 1;
  16.     operation.sem_flg = 0;
  17.     semop (semid, &operation, 1);
  18. }
  19.  
  20. main()
  21. {
  22.     printf("FLAG 2");
  23.     pthread_t num_thread[3];
  24.     /*creation of a semaphore initialised to value 1 */
  25.     semid = semget(12,1,IPC_CREAT|IPC_EXCL|0600);
  26.     semctl(semid,0,SETVAL,1);
  27.     for(i=0; i<3; i++)
  28.     {
  29.         printf("FLAG 3");
  30.         pthread_create(&num_thread[i], NULL, (void *(*)())reservation, NULL);
  31.         pthread_join(num_thread, NULL);
  32.         semctl(semid,0,IPC_RMID,0);
  33.     }
  34.  
Feb 3 '09 #3
JosAH
11,448 Recognized Expert MVP
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
Feb 3 '09 #4
pythoNewbie
10 New Member
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);
Feb 10 '09 #5
JosAH
11,448 Recognized Expert MVP
@pythoNewbie
Unix programmers don't need tutorials; they read the manual pages.

kind regards,

Jos ;-)
Feb 10 '09 #6
pythoNewbie
10 New Member
Aha

I will try this one, thank you very much
Feb 11 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
4339
by: Bastian Hammer | last post by:
Hi I´m wondering why there are so few examples with Semaphore. Is it obsolete? I´ve got a Class Data. It offers 2 Threads methods for updating, editing, .. a private dictionary. Now I...
5
2203
by: clusardi2k | last post by:
Hello, I have a assignment just thrown onto my desk. What is the easiest way to solve it? Below is a brief description of the task. There are multible programs which use the same library...
1
2621
by: rushik | last post by:
Hello all, I m facing an interesting problem related to semaphore in php. We are using a huge business application running on LAMP. For database operations we are maintaining centralized DB...
32
4186
by: William Stacey [MVP] | last post by:
Here is the link. If you find an issue or think of a feature, please post a reply or send an email. Cheers! http://www.mvptools.com/doco/csharp/semaphoredjikstra.htm -- William Stacey, MVP
5
4589
by: marvind | last post by:
I tried using a Semaphore class (have included the full listing reproduced from article Figure 1 at the end of this email) in .NET 1.1. It works fine most of the time, however, I see the following...
5
2494
by: Unni | last post by:
Hello all, I m facing a memory related problem with semaphores. Our business application uses semaphores extensively and the limit imposed by the OS on the number of semaphores that can exist on...
2
2121
by: techi_C | last post by:
Hi I'm getting a problem while removing semaphore from system. Before removing semaphore I'm checking the usage count of a smaphore. // checking usage count usage_count =...
0
951
by: sukasa | last post by:
Greetings. After first noticing a malfunction in my application, I ran some tests which have given me the following insights on the code performance: -The " infinite" loop which waits on...
0
2229
by: Samuel R. Neff | last post by:
I'm having trouble creating a Semaphore with read-access rights for everyone. Originally I was trying to use this code: semaphore = new Semaphore(maxLocks, maxLocks, "RwLock#" + name); but...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.