0

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;
        }
    }

}
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65

1 Answers1

2

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 type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(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.

  • I think maybe the OP intended to use [Cygwin](http://stackoverflow.com/q/985281/1174378)... – Mihai Todor Jul 17 '14 at 16:31
  • Dear duskwuff, thanks for information. As keeping the development tools and the OS is not compulsory, I can implement the same concept using any OS. For Unix, can Linux also support this program? If yes, then Ubunto 10? – Stanish Arul Jagan Jul 17 '14 at 16:41
  • Yes, Linux is one such system. However, mind the notes I'm adding to my post. –  Jul 17 '14 at 17:34