added more code
This commit is contained in:
BIN
CPE435/Lab1/Lab01.pdf
Normal file
BIN
CPE435/Lab1/Lab01.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab1/Lab01_Study_Material.pdf
Normal file
BIN
CPE435/Lab1/Lab01_Study_Material.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab1/demo_1
Executable file
BIN
CPE435/Lab1/demo_1
Executable file
Binary file not shown.
19
CPE435/Lab1/demo_1.cpp
Normal file
19
CPE435/Lab1/demo_1.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
pid_t c_pid;
|
||||
cout << "The pid of the parent is " << getpid() << endl;
|
||||
c_pid = fork();
|
||||
if (c_pid == 0 ) /* we are the child */
|
||||
{
|
||||
cout << "I am the child, my parents pid was " << getppid() << endl;
|
||||
exit(0);
|
||||
}
|
||||
cout << "I am the parent, the child's pid is " << c_pid << endl;
|
||||
|
||||
exit(0);
|
||||
}
|
BIN
CPE435/Lab1/demo_2
Executable file
BIN
CPE435/Lab1/demo_2
Executable file
Binary file not shown.
17
CPE435/Lab1/demo_2.cpp
Normal file
17
CPE435/Lab1/demo_2.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
/* This program illustrates the use of fork */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
x = 0;
|
||||
fork();
|
||||
x++;
|
||||
// This should be printed twice, once by the parent and once by the child
|
||||
cout << "I am process "<< getpid() << " and my x is " << x << endl;
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab1/demo_3
Executable file
BIN
CPE435/Lab1/demo_3
Executable file
Binary file not shown.
18
CPE435/Lab1/demo_3.cpp
Normal file
18
CPE435/Lab1/demo_3.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
/* This program illustrates multiple fork operations */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("I am the Parent\n");
|
||||
fork();
|
||||
printf("This is printed by both parent and child\n");
|
||||
fork();
|
||||
printf("This will be printed 4 times\n");
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab1/demo_4
Executable file
BIN
CPE435/Lab1/demo_4
Executable file
Binary file not shown.
25
CPE435/Lab1/demo_4.cpp
Normal file
25
CPE435/Lab1/demo_4.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
/* This program illustrates the death of the parent process before the child is terminated. */
|
||||
/* The child process is now considered an orphan process since the parent is dead. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
printf("I am the child, my ID is %d\n", getpid());
|
||||
printf("I am the child, my parent is %d\n", getppid());
|
||||
printf("The child will now sleep for 10 seconds\n");
|
||||
sleep(10);
|
||||
printf("I am the same child with ID %d, but my parent Id is %d\n", getpid(), getppid());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("I am the parent with ID %d. My parent is %d and my child is %d\n", getpid(), getppid(), pid);
|
||||
sleep(5);
|
||||
}
|
||||
}
|
BIN
CPE435/Lab1/lab1
Executable file
BIN
CPE435/Lab1/lab1
Executable file
Binary file not shown.
20
CPE435/Lab1/lab1.cpp
Normal file
20
CPE435/Lab1/lab1.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
pid_t x;
|
||||
cout << "The pid of the parent is " << getpid() << endl;
|
||||
for(int i=0; i<10; i++){
|
||||
x = fork();
|
||||
if (x == 0 ) /* we are the child */
|
||||
{
|
||||
cout << "Id: " << getpid() << "\tX: x "<< endl;
|
||||
exit(0);
|
||||
}
|
||||
// cout << "I am the parent, the child's pid is " << x << endl;
|
||||
}
|
||||
exit(0);
|
||||
}
|
3
CPE435/Lab1/lab1Questions.txt
Normal file
3
CPE435/Lab1/lab1Questions.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Question 1: The processes' PIDs are different each time the program is run, and both processes differ by 1.
|
||||
2: Four are running.
|
||||
3:
|
Reference in New Issue
Block a user