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> -
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.
6 9336
I don't see any printf statements in your code.
/*printf*/ is a comment.
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 -
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);
-
}
-
}
-
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
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);
Aha
I will try this one, thank you very much
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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...
| |
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...
|
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 =...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |