File: thread.cc

package info (click to toggle)
ccbuild 2.0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,376 kB
  • ctags: 551
  • sloc: cpp: 5,076; ansic: 1,913; sh: 1,342; makefile: 67
file content (48 lines) | stat: -rw-r--r-- 1,266 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
  This file is part of ccbuild.
  Copyright (C) 2013  A. Bram Neijt <bneijt@gmail.com>

  ccbuild is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  ccbuild is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with ccbuild.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>

int main(int argc, char * argv[])
{
	using namespace std;
	int status;
	pid_t p = fork();
	if(p == -1)
		cout << "Failure to fork\n";
	else if(p == 0)
	{
		cout << "Child exiting with 10\n";
		sleep(2);
		exit(5);
	}
	else
	{
		//Mother waits...
		pid_t pp = wait(&status);
		if(pp == p)
			cout << "Got the pid back from wait\n";
		else
			cout << "Got another pid back from wait: " << pp << "\n";
		cout << "Status: " << WEXITSTATUS(status) << "\n";
	}
	return 0;
}