示例#1
0
文件: main.c 项目: sunwxg/monitor_ssh
void start(void)
{
	pid_t pid;

	daemon(1, 0);

	chdir("/tmp/");

	while (1) {
		pid = fork();

		if (pid == 0) {
			child_process();
			break;

		} else if (pid > 0) {
			parent_process(pid);

		} else {
			print_log("fork error");
			exit(1);
		}
	}

	exit(0);
}
示例#2
0
int main (int argc, char* argv[])
{
	int pongPipe[2];
	int pingPipe[2];
	pid_t child;
	// initialise le pipe.
	pipe(pongPipe);
	pipe(pingPipe);
	// divise le processus.
	child = fork();
	if(child == -1){
		// décris l'erreur.
		perror("Fork error");
		// ferme les descripteur ouvert.
		close(pingPipe[1]);		close(pongPipe[0]);
		close(pongPipe[1]);		close(pingPipe[0]);
		// arrêt suite à une erreur.
		exit(EXIT_FAILURE);
	}

	if(child == 0){
		// lance le code du fils.
		_exit(child_process(pongPipe, pingPipe));
	}
	else{
		// lance le code du père.
		exit(father_process(child, pongPipe, pingPipe));
	}
}
示例#3
0
文件: shm.c 项目: waterylife/SFC
int main()
{
	if(-1 == tell_wait()) {
		printf("fail to tell_wait\n");
		return 0;
	}

	pid_t pid = fork();
	if(0 > pid) {
		printf("fail to fork, errno: %d, errmsg: %s\n", errno, strerror(errno));
		return 0;
	}
	else if(0 == pid) {
		child_process();
		_exit(0);
	}

	parent_process();
	int status;
	if(-1 == wait(&status)) {
		printf("[P]fail to wait for child process, errno: %d, errmsg: %s\n", errno, strerror(errno));
	}

	return 0;
}
示例#4
0
文件: main.c 项目: Iron-Bound/trinity
/* Generate children*/
static void fork_children(void)
{
	while (shm->running_childs < max_children) {
		int childno;
		int pid = 0;

		if (shm->spawn_no_more == TRUE)
			return;

		/* a new child means a new seed, or the new child
		 * will do the same syscalls as the one in the child it's replacing.
		 * (special case startup, or we reseed unnecessarily)
		 */
		if (shm->ready == TRUE)
			reseed();

		/* Find a space for it in the pid map */
		childno = find_childno(EMPTY_PIDSLOT);
		if (childno == CHILD_NOT_FOUND) {
			outputerr("## Pid map was full!\n");
			dump_childnos();
			exit_main_fail();
		}

		fflush(stdout);
		pid = fork();

		if (pid == 0) {
			/* Child process. */
			init_child(childno);
			child_process();
			debugf("child %d %d exiting.\n", childno, getpid());
			close_logfile(&this_child->logfile);
			_exit(EXIT_SUCCESS);
		} else {
			if (pid == -1) {
				/* We failed, wait for a child to exit before retrying. */
				if (shm->running_childs > 0)
					return;

				output(0, "couldn't create child! (%s)\n", strerror(errno));
				panic(EXIT_FORK_FAILURE);
				exit_main_fail();
			}
		}

		shm->children[childno]->pid = pid;
		shm->running_childs++;

		debugf("Created child %d (pid:%d) [total:%d/%d]\n",
			childno, pid, shm->running_childs, max_children);

		if (shm->exit_reason != STILL_RUNNING)
			return;

	}
	shm->ready = TRUE;

	debugf("created enough children\n");
}
示例#5
0
文件: test.c 项目: 292388900/hhvm
int main(void)
{
    int sock[2];

    if(socketpair(PF_UNIX, SOCK_STREAM, 0, sock)) {
	perror("socketpair");
	exit(1);
    } else {
	printf("Established socket pair: (%d, %d)\n", sock[0], sock[1]);
    }

    switch(fork()) {
	case 0:
	    close(sock[0]);
	    child_process(sock[1]);
	    break;
	case -1:
	    perror("fork");
	    exit(1);
	default:
	    close(sock[1]);
	    parent_process(sock[0]);
	    wait(NULL);
	    break;
    }
    return(0);
}
示例#6
0
void
do_command(entry *e, user *u) {
    Debug(DPROC, ("[%ld] do_command(%s, (%s,%ld,%ld))\n",
                  (long)getpid(), e->cmd, u->name,
                  (long)e->pwd->pw_uid, (long)e->pwd->pw_gid));

    /* fork to become asynchronous -- parent process is done immediately,
     * and continues to run the normal cron code, which means return to
     * tick().  the child and grandchild don't leave this function, alive.
     *
     * vfork() is unsuitable, since we have much to do, and the parent
     * needs to be able to run off and fork other processes.
     */
    switch (fork()) {
    case -1:
        log_it("CRON", getpid(), "error", "can't fork");
        break;
    case 0:
        /* child process */
        acquire_daemonlock(1);
        child_process(e);
        Debug(DPROC, ("[%ld] child process done, exiting\n",
                      (long)getpid()));
        _exit(OK_EXIT);
        break;
    default:
        /* parent process */
        break;
    }
    Debug(DPROC, ("[%ld] main process returning to work\n",(long)getpid()));
}
示例#7
0
/*************************************************************************
 * \brief main
 *************************************************************************/
int main(int argc, char* argv[])
{
    pid_t pid;

    pid = fork();
    
    if (pid == 0)
    {
        child_process();
    }
    else
    {
    	  printf("Parent process id is %d\n", getpid());
    	  
        while(1)
        {
    	      printf(" @P@: Child process child_count is %d\n", child_count);
    	      printf(" @P@: Parent process parent_count is %d\n", parent_count);
    	      printf("\n");
    	      
    	      parent_count++;
    	      
    		    sleep(3);
        }
    }
    
    return 0;
}
示例#8
0
文件: main.c 项目: kcoewoys/project
int main(int argc, const char *argv[])
{
	pid_t pid;

	 CLEAR();
	 HIDE_CURSOR();

	//开启终端读取一个字符的功能
	disable_terminal_return();

	//创建一个线程
	pid = fork();
	PERROR("fork", pid);

	//进入父进程//控制游戏的基本运行
	if (pid > 0)
	{
		//延时1秒,等待子进程就绪
		sleep(1);
		father_process();
	}

	//进入子进程//控制玩家的操作
	if (pid == 0)
	{	
		child_process();
	}

	CLEAR();
	RESET_CURSOR();
	SHOW_CURSOR();

	return 0;
}
示例#9
0
static int start_child_process(apr_pool_t *p, server_rec *server, struct global_config_data *d) {
    apr_proc_t* proc;
    apr_status_t status;

/*     ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Spawning child pid=%lu", (unsigned long) getpid()); */

    proc = apr_palloc(p, sizeof(apr_proc_t));
    ap_assert(proc);

    switch (status = apr_proc_fork(proc, p)) {

        case APR_INCHILD:
            child_process(p, server, d);
            exit(1);
            /* never reached */
            break;

        case APR_INPARENT:
            apr_pool_note_subprocess(p, proc, APR_KILL_ONLY_ONCE);
/*             ap_log_error(APLOG_MARK, APLOG_NOTICE, status, server, "Child process %lu", (unsigned long) proc->pid); */

            break;

        default:
            ap_log_error(APLOG_MARK, APLOG_ERR, status, server, "apr_proc_fork() failed");
            return HTTP_INTERNAL_SERVER_ERROR;
    }

    return OK;
}
示例#10
0
void		parent_process(char **env)
{
  pid_t		pid;
  char		*s;
  char		**tab;

  while ((s = get_next_line(0)) != NULL)
    {
      if (my_strcmp("exit", s) == 0)
	exit(EXIT_SUCCESS);
      signal(SIGINT, SIG_IGN);
      tab = my_str_to_wordtab(s);
      my_cd(s, tab, env);
      if (s[0] != 'c' || s[1] != 'd')
	pid = fork();
      if (pid == 0 && (s[0] != 'c' || s[1] != 'd'))
        child_process(s, tab, env);
      else if (pid == -1)
        my_error("Impossible to create a child process");
      else
        {
	  waitpid(pid, 0, WSTOPPED);
          my_putstr("$> ");
        }
      free(s);
      free(tab);
    }
}
示例#11
0
void execute_program(char *program, char *argv[], char *envp[], int input_count, char *input[], char **output)
{
    int fd_stdin[2];
    int fd_stdout[2];

    if(-1 == pipe(fd_stdout)) {
        FLOCK_TRACE("Error: Failed to create pipe for stdout \n");
        exit(1);
    }
    
    if(-1 == pipe(fd_stdin)) {
        FLOCK_TRACE("Error: Failed to create pipe for stdin\n");
        exit(1);
    }
   
    pid_t  pid = fork();
    if(pid== -1) {        
        FLOCK_TRACE("Error: Failed to create a process\n");        
        exit(1);
    } else if(pid == 0) { //Child process gets pid as zero
        FLOCK_TRACE("Child process created");        
        child_process(fd_stdin, fd_stdout, program, argv, envp);        
    } else if(pid > 0) {  // parent process gets child_pid
        FLOCK_TRACE("Parent process");
        parent_process(pid, fd_stdin, fd_stdout, input_count, input, output);
    }
}
示例#12
0
void CrashHandlerHandleArgs( int argc, char* argv[] )
{
	g_pCrashHandlerArgv0 = argv[0];
	if( argc == 2 && !strcmp(argv[1], CHILD_MAGIC_PARAMETER) )
	{
		child_process();
		exit(0);
	}
}
示例#13
0
int
dsm_start() {
    spawn_processes();

    dsm_init(proc_id);
    start_server_thread();
    child_process();

    return 0;
}
示例#14
0
void main() {
  pid_t pid;

  pid = fork();
  if(pid == 0) {
    child_process();
  }
  else {
    parent_process();
  }
}
示例#15
0
int xdisp_start(void)
{
	unsigned int cnum;

	/*
	 * find free client struct
	 */
	for (cnum = 0; (cnum < NUMCLI) && cli[cnum].used; cnum++);
	if (cnum >= NUMCLI)
		return -1;
	signal(SIGCHLD, sigchld_handler);
	/*
	 * start "IPC" mechanism (using the pipes)
	 */
	if (pipe(cmdpipe))
		return -1;
	if (pipe(datapipe)) {
		close(cmdpipe[0]);
		close(cmdpipe[1]);
	}
	if ((cli[cnum].pid = fork())) {
		if (cli[cnum].pid == (pid_t)-1) {
			/* error */
			close(cmdpipe[0]);
			close(cmdpipe[1]);
			close(datapipe[0]);
			close(datapipe[1]);
			return -1;
		}
		/* parent */
		cli[cnum].cmdfd = cmdpipe[0];
		close(cmdpipe[1]);
		close(datapipe[0]);
		cli[cnum].datafd = datapipe[1];
		cli[cnum].used = 1;
		fcntl(cmdpipe[0], F_SETFL, 
		      (fcntl(cmdpipe[0], F_GETFL, 0) | O_NDELAY));
		return cnum;
	}
	/*
	 * child; the X process
	 */
	close(cmdpipe[0]);
	close(datapipe[1]);
	close(0);  /* close stdin */
	child_win_init();
	child_process();
	exit(0);
}
示例#16
0
int main(int argc, char const *argv[])
{
  int newpid;

  printf("Before: pid is %d\n", getpid());

  if ((newpid = fork()) == 0)
  {
    child_process(DELAY);
  }
  else
  {
    parent_process(newpid);
  }
  return 0;
}
示例#17
0
文件: 2-1.c 项目: Mellanox/arc_ltp
int main()
{
	char mqname[NAMESIZE];
	pid_t pid;
	int to_parent[2];
        int to_child[2];
	int rval;
	struct sigaction sa;

	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGCHLD, &sa, NULL);

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());
	rval = pipe(to_parent);
        if (rval == -1) {
                perror(ERROR_PREFIX "fd[0]");
                return PTS_UNRESOLVED;
        }
        rval = pipe(to_child);
        if (rval == -1) {
               perror(ERROR_PREFIX "fd[1]");
               return PTS_UNRESOLVED;
        }
	pid = fork();
	if (pid == -1) {
		perror(ERROR_PREFIX "fork");
		return PTS_UNRESOLVED;
	}
	if (pid == 0) {
		//child process
		close(to_parent[PIPE_READ]);
		close(to_child[PIPE_WRITE]);
		return child_process(mqname, to_child[PIPE_READ],
                                     to_parent[PIPE_WRITE]);
	}
	else {
		//parent process
		close(to_parent[PIPE_WRITE]);
                close(to_child[PIPE_READ]);
		return parent_process(mqname, to_parent[PIPE_READ],
                                      to_child[PIPE_WRITE], pid);
	}
}
int bruteforce(unsigned long firstret)
{
int found;
long i;
unsigned long ret;
fprintf(stdout,"[+] Bruteforce Starting!!!\n");
fprintf(stdout,"firstret = %lu\n",firstret);
for(i = firstret ; i<0 ; i+=DEC)
     {
     fprintf(stdout,"[+] Testing Ret Address 0x%x\n",i);
     make_buffer(i);
     found = child_process();
      if(found == 0)
        { printf("Ret Adress Found = 0x%x\n",i);
	  break;
	}
    }
   return(0);
 }   
示例#19
0
void daemonify(void)
{
	pid_t pid, sid;
	int fd;
	if((pid = fork()) < 0) {
		exit(EXIT_FAILURE);
	} else if(pid != 0) {
		printf("parent pid = %d\n", pid);
		/* close the parent process */
		sleep(1);
		exit(EXIT_SUCCESS);
	}
	printf("%d",__LINE__);
	umask(027);
	printf("%d",__LINE__);
	sid = setsid(); /* get a new process group. */

	printf("%d",__LINE__);
	if(sid < 0) {
		/*failure to deamonify*/
		exit(EXIT_FAILURE);
	}

	printf("%d",__LINE__);
	chdir("/");
	umask(0);

	printf("%d",__LINE__);
	/* Close out the standard file descriptors */
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);

	printf("%d",__LINE__);
	fd = open("/dev/null", O_RDWR);/* stdin */
	(void) dup(fd);  /* stdout */
	(void) dup(fd);  /* stderr */

	printf("child pid = %d\n", pid);
	child_process();

}
 main(int argc, char *argv[])
 {
  char *optionlist = "ba:h:";
  int option;
  unsigned long start = BRUTE_START;
  unsigned long  choose;
  int u_r_script_kiddy = 0;
  int Opterr = 1;
  banner(argv[0]);
  if(argc < 2)
  fprintf(stderr,"Use -h for help\n");
  while( (option = getopt(argc,argv,optionlist) ) != -1)
       switch(option)
        { 
	  case 'b':
	   u_r_script_kiddy=1;
	   break;
	  
	  case 'h':
	   banner(argv[0]);
	   break; 
	   
	  case 'a':
           choose = strtoul(optarg,NULL,0);
           make_buffer(choose);
           child_process();
           exit(0);
           break;
	  
	  case '?':
	   fprintf(stderr,"Unknown Option \n");
	   banner(argv[0]);
	   exit(-1);
	   
	  default:
	   banner(argv[0]);
	   exit(-1);
	}
   if(u_r_script_kiddy)
     bruteforce(start);
      return 0;
}    
示例#21
0
int main(int argc, char *argv[]) {
   /* Programma che genera un processo figlio e che poi termina prima del figlio
   stesso, rendendolo orfano; attenzione, non si sta parlando di zombie, lo 
   scopo e' di far adottare l'orfano da init. */

   pid_t pid;
   pid = fork(); 

   if (pid == -1) {
      fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
      exit(EXIT_FAILURE);
   }

   if (pid == 0)
      child_process(pid);
   else
      parent_process(pid);

   return(EXIT_SUCCESS);
}
示例#22
0
int
main(void)
{
    kern_return_t  kr;
    int            status;
    mach_port_t    mytask = mach_task_self();
    mach_vm_size_t size = (mach_vm_size_t)vm_page_size;
   
    kr = mach_vm_allocate(mytask, &page_shared, size, VM_FLAGS_ANYWHERE);
    OUT_ON_MACH_ERROR("vm_allocate", kr);
   
    kr = mach_vm_allocate(mytask, &page_cow, size, VM_FLAGS_ANYWHERE);
    OUT_ON_MACH_ERROR("vm_allocate", kr);
   
    kr = mach_vm_inherit(mytask, page_shared, size, VM_INHERIT_SHARE);
    OUT_ON_MACH_ERROR("vm_inherit(VM_INHERIT_SHARE)", kr);
   
    kr = mach_vm_inherit(mytask, page_cow, size, VM_INHERIT_COPY);
    OUT_ON_MACH_ERROR("vm_inherit(VM_INHERIT_COPY)", kr);
   
    FIRST_UINT32(page_shared) = (unsigned int)0xAAAAAAAA;
    FIRST_UINT32(page_cow)    = (unsigned int)0xBBBBBBBB;
   
    printf("%-12s%-8s%-10s%-12s%-10s%s\n",
           "Process", "Page", "Contents", "VM Object", "Refcount", "Event");
   
    peek_at_some_memory("parent", "before forking");
   
    if (fork() == 0)
        child_process(); // this will also exit the child
   
    wait(&status);
   
    peek_at_some_memory("parent", "after child is done");
   
out:
    mach_vm_deallocate(mytask, page_shared, size);
    mach_vm_deallocate(mytask, page_cow, size);
   
    exit(0);
}
示例#23
0
int main (int argc, char *argv[])
{
	struct arg_t *kom_arg, arg;
	pid_t child_pid;
	int fifofd1;

	/* Setup a signal handler for SIGINT */
	signal(SIGINT, int_proc);
	signal(SIGTERM, int_proc);

	kom_arg = &arg;
	Cmdline (kom_arg, argc, argv);

	if (kom_arg->v)
	{
		printf ("%s: version %s\n", argv[0], PACKAGE_VERSION);
		return EXIT_FAILURE;
	}

	verbose = kom_arg->V; 

	if (verbose) printf ("start\n");

	fifofd1 = fifo_create (FILENAME, 4096, 0);

	child_pid = fork ();
	if (child_pid == 0) child_process ();

	while (!stop)
	{
		fifo_write (fifofd1, "Hallo", 5);
		printf ("write\n");
		sleep (1);
	}

	fifo_destroy (fifofd1);

	if (verbose) printf ("done\n");

	return EXIT_SUCCESS;
}
示例#24
0
文件: server.cpp 项目: amumu/gaston_c
int fork_process(int num) {
    int i, k;
    int pid = -1;
    for (i = 0; i < num; i++) {
        if (pid != 0) {
            ForkPos++;
            pid = fork();
            printf(":::%d\n", pid);
            if (pid == 0) {
                child_process();
                break;
            } else {
                //waitpid(pid,NULL,0);
            }
        } else {

            //printf("This is parent process[%i]\n",pid);
        }
    }
    return pid;
}
示例#25
0
文件: fork.c 项目: abijith-kp/os_lab
int main(int argc , char **argv[])
	{
 	  printf("\nmain()\n");
 	  int id;
 	  id = fork();

	   if(id == 0) 
   		{ //printf("\nchild process : %d\n" , getpid());
        	 //fork();
		 child_process();
		}
   
 	  else if (id > 0)
   		{ //printf("\nparent process : %d\n" , getpid());
		 //wait();
		 parent_process();
   		}

   	printf("\nend of main : %d\n" , getpid());
   	return 0;
	}	
示例#26
0
void
do_command (entry * e, user * u)
{
  Debug (DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
		 getpid (), e->cmd, u->name, e->uid, e->gid));
  /* fork to become asynchronous -- parent process is done immediately,
   * and continues to run the normal cron code, which means return to
   * tick().  the child and grandchild don't leave this function, alive.
   *
   * vfork() is unsuitable, since we have much to do, and the parent
   * needs to be able to run off and fork other processes.
   */
  switch (fork ())
    {
    case -1:
      log_it ("CRON", getpid (), "error", "can't fork");
      break;
    case 0:
      /* child process */
      acquire_daemonlock (1);
      child_process (e, u);
      Debug (DPROC, ("[%d] child process done, exiting\n", getpid ()));
#ifndef USE_SIGCHLD
      /* remember current time. This is not entirely correct because
       * the job might have been a catch-up job in that case we
       * should only save its timestamp but we don't have access to
       * the parent's CatchUpList variable
       * we only have to do this here if we don't use SIGCHLD, otherwise
       * the timestamp is saved correctly in our parent's sigchld_handler.
       */
      save_lastrun (NULL);
#endif
      _exit (OK_EXIT);
      break;
    default:
      /* parent process */
      break;
    }
  Debug (DPROC, ("[%d] main process returning to work\n", getpid ()));
}
示例#27
0
void sample_start()
{
	pid_t pid;
	int pipefd[2];

	/* create pipe first */
	if (pipe(pipefd) < 0) {
		perror("pipe");
		return;
	}

	/* then fork child process */
	pid = fork();
	if (pid < 0) {
		perror("fork");
		return;
	}

	if (pid > 0)
		child_process(pipefd);
	else
		parent_process(pipefd);
}
int main(void) {  
    pid_t result_pid;  
  
    struct termios oldtio, newtio;  
    char buf[255];  
  
    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);  
    if (fd < 0) {  
        perror(MODEMDEVICE);  
        return (-1);  
    }  
  
    tcgetattr(fd, &oldtio);
    memset(&newtio, 0, sizeof(newtio));  
  
    // ready serial port  
    serial_init(fd);  
  
    //fork  
    result_pid = fork();  
  
    if (result_pid == -1) {  
        fprintf(stderr, "fork failed.\n");  
        return COULDNOTFORK;  
    }  
  
    if (result_pid == 0) {  
        child_process();  
    } else {  
        fprintf(stderr, "fork completed");  
  
        parent_process(result_pid);  
    }  
    STOP = TRUE;  
  
    return 0;  
}  
示例#29
0
static void fork_children()
{
	unsigned int i;

	/* Generate children*/

	while (shm->running_childs < shm->nr_childs) {
		int pid = 0;

		/* Find a space for it in the pid map */
		for (i = 0; i < shm->nr_childs; i++) {
			if (shm->pids[i] == -1)
				break;
		}
		if (i >= shm->nr_childs) {
			output("pid map full!\n");
			exit(EXIT_FAILURE);
		}
		(void)alarm(0);
		pid = fork();
		if (pid != 0)
			shm->pids[i] = pid;
		else {
			int ret = 0;

			ret = child_process();
			shm->regenerate--;
			output("child %d exitting\n", getpid());

			_exit(ret);
		}
		shm->running_childs++;
		debugf("[%d] Created child %d [total:%d/%d]\n", getpid(), shm->pids[i], shm->running_childs, shm->nr_childs);
	}
	debugf("[%d] created enough children\n\n", getpid());
}
示例#30
0
int main() {
	int fd, child_pid;
	char *buf;

	child_pid = fork();
	if(child_pid == -1) {
		perror("An error occurs when calling fork()");
		return PTS_UNRESOLVED;
	} else if(child_pid == 0) {
		return child_process();
	}
	
	wait(NULL);

	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR|S_IWUSR);
	if(fd == -1) {
		perror("An error occurs when calling shm_open()");
		return PTS_UNRESOLVED;
	}

	buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0);
	if( buf == MAP_FAILED) {
		perror("An error occurs when calling mmap()");
		return PTS_UNRESOLVED;	
	}	

	shm_unlink(SHM_NAME);

	if(strcmp(buf, str) == 0) {
		printf("Test PASSED\n");
		return PTS_PASS;
	}

	printf("Test FAILED\n");
	return PTS_FAIL;
}