#include <iostream> #include <cstdlib> #include <signal.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <fstream> #define NUM_THREADS 10 using namespace std; void *print_hello(void *threadid) { char path_file[50]; long tid; pthread_t self; self = pthread_self(); tid = (long)threadid; sprintf(path_file,"/disk2/home/arpit/myprog/my_worker_thread_%d",self); fstream logfile(path_file,std::ios::in|std::ios::out); // cout << "thread number: "<< self << endl; for(int i=0;i<20;i++) { logfile << "thread number: "<< self<<" " << "print hello " << endl; } return 0; } int main() { int i,rc; long t=0; pthread_t threads[NUM_THREADS]; int pid_a = fork(); if(pid_a == 0) { for(i = 0; i <10000; i++) { //cout << i << endl; if(i==1000) { cout << "IN WORKER ENGINE" << endl; sleep(2); pid_t id = getpid(); for(int worker = 0;worker < NUM_THREADS ; worker++) { rc = pthread_create(&threads[worker], NULL, print_hello, (void *)t); if (rc) { cout << "ERROR; return code from pthread_create() is " << rc << "for worker no :" << worker << endl; exit(-1); } } for(int worker = 0;worker < NUM_THREADS ; worker++) { pthread_join(threads[worker],NULL); } cout << "child process being killed:" <<id<< endl; if(id > 0) kill(id,9); } } } else { pid_t id1 = getpid(); cout << "IN MASTER ENGINE " << id1 << endl; sleep(2); cout << "i am in master engine" << endl; cout << "i am in master engine" << endl; } return 0; }