简体   繁体   English

当进程进入信号灯(关键部分)并进入休眠状态时会发生什么?

[英]What happens when a process enters a semaphore (critical section) and sleeps?

As per my understanding, when a process enters a critical section, no other process can simultaneously enter. 根据我的理解,当一个进程进入关键部分时,没有其他进程可以同时进入。 But i see, by a program, that it is not. 但是我看到,通过一个程序,事实并非如此。

I create Process A, and child Process B. Child enters critical section, and sleeps, meanwhile i am surprised to see that parent too enters critical section, while child sleeps. 我创建了流程A,孩子创建了流程B。孩子进入关键部分并入睡,与此同时,我很惊讶地看到父母也进入了关键部分,而孩子却入睡。 How is it possible? 这怎么可能? 2 processes simultaneously at critical section? 关键部分同时进行2个过程?

enter code here
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>

sem_t sem;
int shared=0;
int pid;

void func()
{
 sem_trywait(&sem);
 if(pid==0)printf("Child entered\n");
 else if(pid>0)printf("Parent entered\n");
 sleep(2);
 shared++;
 sem_post(&sem);
 if(pid==0)printf("Child exited\n");
 else if(pid>0)printf("Parent exited\n");
}

int main()
{
 pid=fork();
 sem_init(&sem,1,0);
 if(pid==0){
  printf("In child\n");
  func();
 }
 else {
 func();
}
}
Output:
 [root@dhcppc0 semaphore]# gcc semaphore1.c -lrt
 [root@dhcppc0 semaphore]# ./a.out
  In child
  Child entered
  Parent entered

  <pause 2 secs>

  Child exited
  Parent exited

For a semaphore to work across processes, it needs to reside in shared memory and to be initialized with pshared==1 - You're not putting the semaphore in shared memory. 为了使信号量跨进程工作,它必须驻留在共享内存中,并使用pshared==1进行初始化-您无需将信号量放入共享内存中。 Look up eg shm_open or mmap . 查找例如shm_openmmap

You should also initialize the semaphore before you fork() - initializing a sempahore twice doesn't work. 您还应该在fork()之前初始化信号量-两次初始化sempahore无效。 Also use sem_wait rather than sem_trywait as you seem to want to block on the semaphore. 也可以使用sem_wait而不是sem_trywait因为您似乎想阻止信号量。 If you want sem_trywait at least check if the try part succeeded. 如果要sem_trywait至少检查try部分是否成功。

EDIT: Corrected source. 编辑:更正的来源。

#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>

sem_t * sem; /* MODIFIED: We want a semaphore in shared memory, using a pointer instead */
int shared=0;
int pid;

void func()
{
 sem_wait(sem); /* MODIFIED &sem to sem */
 if(pid==0)printf("Child entered\n");
 else if(pid>0)printf("Parent entered\n");
 sleep(2);
 shared++;
 sem_post(sem); /* MODIFIED &sem to sem */
 if(pid==0)printf("Child exited\n");
 else if(pid>0)printf("Parent exited\n");
}

int main()
{
  /* MODIFIED: Put semaphore in shared memory */
  sem = mmap(0, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 /* MODIFIED: Initial count of 1, so that a sem_wait will succeed */
 sem_init(sem,1,1);
 /* MODIFIED: fork() after sem_init() */
 pid=fork();
 if(pid==0){
  printf("In child\n");
  func();
 }
 else {
 func();
}
}

And check the return values of the sem_* functions! 并检查sem_ *函数的返回值! From the man page: 从手册页:

The sem_trywait() function shall lock the semaphore referenced by sem only if the semaphore is currently not locked; sem_trywait()函数仅在当前未锁定信号量的情况下,才应锁定sem引用的信号量。 that is, if the semaphore value is currently positive. 也就是说,如果信号量值当前为正。 Otherwise, it shall not lock the semaphore. 否则,它不应锁定信号灯。

So if you don't check what it returns, you don't know if you've locked anything at all. 因此,如果不检查返回的内容,就根本不知道是否锁定了任何内容。

You are using sem_trywait function,then you should check the value returned by this call so as to ensure sysnchronization... 您正在使用sem_trywait函数,然后应检查此调用返回的值,以确保系统同步...

Can refer this for more help.... 可以参考以获得更多帮助。

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当两个进程试图访问信号量= 0的关键部分时会发生什么? - What happens when two processes are trying to access a critical section with semaphore = 0? C信号量生产者-消费者关键部分 - C semaphore producer-consumer critical section 两个进程访问关键部分时出错 - Error with two process access to critical section 文件系统已满时进程会发生什么 - What happens to a process when the filesystem is full 当父进程调用exec命令时,子进程会发生什么情况 - What happens to the child process, when the parent process calls an exec command 当等待进程(在信号量队列中)接收到来自另一个进程的信号时会发生什么? - What happened when a waiting process (in semaphore queue) receives a signal from another process? 在队列的关键部分使用二进制信号量而不是互斥量进行互斥是否有任何优势? - Are there any advantages to using a binary semaphore instead of a mutex for mutual exclusion in a critical section of a queue? 当进程结束时,Sleep()中间的线程会发生什么? - When a process ends, what happens to a thread that is in the middle of Sleep()? 视觉上当您使用&符号调用相同的进程时会发生什么 - Visually what happens when you call same process with ampersand zmq-当进程终止时,如何拉出队列? - zmq - what happens to pull queue when process dies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM