Demo entry 6734679

c

   

Submitted by anonymous on Apr 21, 2018 at 11:08
Language: C. Code size: 852 Bytes.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int glob = 88;               //a global var
void foo(int *);

int main(int argc,char *arg[])
{
    int var = 100;            //a local var in main
    foo(&var);
    if(printf("In main var:%d  glob:%d pid:%d\n",var,glob,getpid())<0)
        perror("main printf");
    exit(0);
}

void foo(int *v)
{
    pid_t pid;
    int loc = 66;                 //a local var in foo
    printf("Before vfork\n");
    if((pid = vfork())<0)
        perror("vfork");
    else if(pid == 0)            //child process
    {
        loc++;
        (*v)++;
        glob++;
        printf("pid:%d\n",getpid());
        exit(0);
    }
         /*parent process continues here*/
    printf("In foo var:%d  glob:%d  loc:%d  pid:%d\n",*v,glob,loc,getpid());

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).