473,756 Members | 6,106 Online
Bytes | Software Development & Data Engineering Community
+ 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<phtrea d.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 9370
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
4356
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 have to make sure, that both threads are synchronal,
5
2227
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 routine which is an interface to what I'll call a service program.
1
2646
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 manager classes in php which perform all the single table related activity. Before inserting new record we are acquiring semaphore on specific key (all the tables are having seprate sem keys), insert the record and release the semaphore.
32
4231
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
4607
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 error occassionally: 08/28/2005 17:32:42.82, Verbose, QueryObject.MergeSubQuery, Exception: Too many posts were made to a semaphore in mscorlib. Server stack trace:
5
2523
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 the system is only 128. This could be increased to a higher value but that would not be a permanant solution to my belief. We are using the sem_get, sem_acquire php functions. After the use we are releasing the semaphore by using sem_release.
2
2146
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 = semctl(sem_ptr->semid, 1, GETVAL, NULL); if( usage_count 1 ) // return don't remove semaphore else
0
971
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 the semaphore first thing in every iteration, may succeed in going through several iterations. -The number of successful iterations may differ with every execution, as well as with timing differences caused by the different placement of breakpoints. ...
0
2251
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 when using that code to create a Semaphore instance for an existing system semaphore (i.e., the second time the code gets hit), I get an UnauthorizedAccessException. So I tried specifying that everyone has read rights:
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10028
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9707
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6533
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3804
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 we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.