#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <string>
#include <semaphore.h>
using namespace std;
int main(int argc, char *argv[]){
int pshared = 1;
unsigned int value = 0;
sem_t sem_name;
sem_init(&sem_name, pshared, value);
int parentpid = getpid();
pid_t pid = fork();
if (parentpid == getpid()){
cout << "parent id= " << getpid() << endl;
sem_wait(&sem_name);
cout << "child is done." << endl;
}
if (parentpid != getpid()){
cout << "child id= " << getpid() << endl;
for (int i = 0; i < 10; i++)
cout << i << endl;
sem_post(&sem_name);
}
sleep(4);
return 0;
}
परिणाम होना चाहिए:सैमफोर क्यों काम नहीं कर रहा है?
parent id 123456.
child id 123457.
0
1
2
3
4
5
6
7
8
9
child is done.
कार्यक्रम बाहर निकलता है, लेकिन इसके बजाय यह सेमाफोर का संकेत नहीं है।
में आपका स्वागत है लिया! हमारे पास आपका कोड है और परिणाम क्या होना चाहिए ... वास्तविक परिणाम क्या है? आप कैसे सुनिश्चित हैं कि सेमफोर कभी संकेत नहीं दिया जाता है? क्या आप इसे जोड़ने के लिए अपना प्रश्न संपादित कर सकते हैं। अब तक मैं कुछ भी अपने कोड –