Skip to content

Instantly share code, notes, and snippets.

@bongbongco
Last active December 18, 2016 06:44
Show Gist options
  • Save bongbongco/b770b3990d611a579d83d3bae41c49e8 to your computer and use it in GitHub Desktop.
Save bongbongco/b770b3990d611a579d83d3bae41c49e8 to your computer and use it in GitHub Desktop.
리눅스 쓰레드 생성
#define CLONE_VM 0x00000100 /* set if VM shared between processes */
#define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */
#define CLONE_THREAD 0x00010000 /* Same thread group? */
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <sched.h>
int g = 2;
int sub_func(void *arg)
{
g++;
printf("PID(%d) : Child g=%d \n", getpid(), g);
sleep(2);
return 0;
}
int main(void)
{
int child_stack[4096];
int l = 3;
printf("PID(%d): Parent g=%d, l=%d \n", getpid(), g, l);
clone(sub_func, (void *)(child_stack+4095), CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, NULL);
sleep(1);
printf("PID(%d) : Parent g=%d, l=%d \n", getpid(), g, l);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment