Skip to content

Instantly share code, notes, and snippets.

@guilhermesilveira
Created October 3, 2013 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermesilveira/6810743 to your computer and use it in GitHub Desktop.
Save guilhermesilveira/6810743 to your computer and use it in GitHub Desktop.
1. C언어 Stack and Dynamic allocation test when using fork 2. C언어 보무프로세스 죽는 test
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid, pid2;
if( (pid = fork()) < 0 ) {
perror("fork failed");
}
else if( pid == 0 ) {
printf("child process %d alive\n", getpid() );
if((pid2 = fork()) < 0) {
perror("child fork failed");
} else if(pid2 == 0) {
printf("grandchild %d started\n", getpid());
sleep( 30 );
printf("grandchild %d dying\n", getpid());
exit(0);
} else {
printf("child process %d dying with 0\n", getpid() );
exit(0);
}
}
else {
printf("parent process : sleeping for 10\n" );
sleep(30);
}
return 0;
}
// C언어 Stack and Dynamic allocation test when using fork
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int shared = 1;
int *shared2 = malloc(sizeof(int) * 1000);
shared2[999] = 37;
if( (pid = fork()) < 0 ) {
perror("fork failed");
}
else if( pid == 0 ) {
sleep( 5 );
printf("child process dying\n" );
printf("stack memory = %d\n", shared);
printf("malloc %p memory = %d\n", &shared2[999], shared2[999]);
exit(0);
}
else {
printf("before parent dies with %d and %d\n", shared, shared2[999]);
shared = 5;
shared2[999] = 42;
printf("parent process dying\n" );
printf("parent memory = %d\n", shared);
printf("parent malloc %p memory = %d\n", &shared2[999], shared2[999]);
exit(0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment