All pastes #940754 Raw Edit

Anonymous

public text v1 · immutable
#940754 ·published 2008-03-13 07:45 UTC
rendered paste body
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h> 
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define NUM_THREADS 2
#define MAX_TEXT 256
#define gettid() syscall(SYS_gettid)
pthread_t threads[NUM_THREADS];
void * start_routine (void * kuka)
{
 char txt[MAX_TEXT];
 int l;
 while(1)
 {
	l=snprintf(txt,MAX_TEXT,"I am tid:%d pid:%d \n", gettid(),getpid());
	while (l!=write(0,txt,l)) {}
	sleep(1);
 }
}
int main(int argv , char *argc[])
{
	char txt[MAX_TEXT];
	int i;
	printf("I am the thread group leader, my tid:%d, pid :%d \n"
		"kill me, or one of my child by SIGSTOP  (SIGCONT to resume)\n",gettid(),getpid());
	for (i=0;i<NUM_THREADS;i++)
	{
	int l;
	if (pthread_create(threads+i, NULL, start_routine, NULL)) 
	{
		perror("pthread_create");
		exit(-1);
	}
	l=snprintf(txt,MAX_TEXT,"Creted thread: %d \n",threads[i]);
	write(0,txt,l);
	}

	start_routine (NULL);
	return 0;
}