My Goal is to get outputs from Critical Section C
Program (Visual Studio 2013 on Windows 7). I get the following errors:
error LNK2019: unresolved external symbol _vfork referenced in function _main
error LNK1120: 2 unresolved externals
My Code is
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
int turn;
int flag[2];
int main(void)
{
int pid, parent = 1;
printf("before vfork\n");
if ((pid = vfork())<0)
{
perror("vfork error\n");
return 1;
}
while (1)
{
if (pid == 0)
{
while (parent == 1)
{
sleep(2);
}
parent = 0;
flag[0] = 1;
turn = 1;
while (flag[1] && turn == 1);
printf("This is critical section:parent process\n");
flag[0] = 0;
}
else
{
parent = 2;
printf("This is parent");
flag[1] = 1;
turn = 0;
while (flag[0] && turn == 0);
printf("This is critical section:child process %d \n", pid);
flag[1] = 0;
}
}
}
Windows does not implement the vfork()
system call; it is generally only available on UNIX systems. You will need to use threads to implement equivalent functionality.
Information on using threads in Windows is available in the Windows Dev Center under " Processes and Threads ".
Please note that, as written, your program invokes undefined behavior . The POSIX.1 specification explains that:
…behavior is undefined if the process created by
vfork()
either modifies any data other than a variable of typepid_t
used to store the return value fromvfork()
, or returns from the function in whichvfork()
was called, or calls any other function before successfully calling_exit(2)
or one of theexec(3)
family of functions.
To make a long story short, you can only use vfork()
to lead up to an exec()
. It cannot safely be used for anything else at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.