Uploaded by Віталій Давидов

практична 01102021

advertisement
практична 01102021
childproc
# include <unistd.h>
# include <iostream>
int main (int argc, char* argv[]){
int tnum;
tnum=atoi(argv[1]);
std::cout<<"hello! i am a child "<<getpid()<<". my job is write "<<tnum<<std::endl;
return EXIT_SUCCESS;
}
proc1
a
# include <unistd.h>
# include <iostream>
# include <sys/types.h>
# include <sys/wait.h>
int main(void){
pid_t pid;
int status;
pid=fork();
switch (pid) {
case -1:
perror("fork don't work");
break;
case 0:
// exersise 1
printf("We work in childprocess\n");
exit(0);
break;
default:
printf("We work in parent process. Child id: %i\n", pid);
getchar();
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
break;
}
return 0;
}
b
# include <unistd.h>
# include <iostream>
# include <sys/types.h>
# include <sys/wait.h>
int main(void){
pid_t pid;
int status;
//exersise 2
char* args[]={(char*)"/home/oksana/labOS/lab2021/childproc", (char*)"5", NULL};
pid=fork();
switch (pid) {
case -1:
perror("fork don't work");
break;
case 0:
// exersise 2
execv("/home/oksana/labOS/lab2021/childproc", args);
printf("We work in childprocess\n");
exit(0);
break;
default:
printf("We work in parent process. Child id: %i\n", pid);
getchar();
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
break;
}
return 0;
}
proc2
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
using namespace std;
int main(void){
pid_t pid;
int status;
cout<<"We work with process"<< getpid()<<endl;
pid=fork();
switch (pid) {
case -1:
perror("fork");
break;
case 0:
//exersise3
execl("/usr/bin/gnome-terminal", "gnome-terminal", "--",
"/home/oksana/labOS/lab2021/childproc", (char*)"5", (char*)0);
printf("We work in childprocess\n");
break;
default:
printf("We work in parent process. Child id: %i\n", pid);
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
break;
}
sleep(1);
return EXIT_SUCCESS;
}
//home_exersise1
/*
extern char **environ;
void test_fork_exec(void);
void test_posix_spawn(void);
int main(void) {
test_fork_exec();
test_posix_spawn();
return EXIT_SUCCESS;
}
void test_fork_exec(void) {
pid_t pid;
int status;
puts("Testing fork/exec");
fflush(NULL);
pid = fork();
switch (pid) {
case -1:
perror("fork");
break;
case 0:
execl("/bin/ls", "ls", (char *) 0);
perror("exec");
break;
default:
printf("Child id: %i\n", pid);
fflush(NULL);
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
break;
}
}
void test_posix_spawn(void) {
pid_t pid;
char *argv[] = {"ls", (char *) 0};
int status;
puts("Testing posix_spawn");
fflush(NULL);
status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, environ);
if (status == 0) {
printf("Child id: %i\n", pid);
fflush(NULL);
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
} else {
printf("posix_spawn: %s\n", strerror(status));
}
}*/
proc3
a
# include <unistd.h>
# include <iostream>
# include <sys/types.h>
# include <sys/wait.h>
int main(void){
pid_t pid;
int status;
printf("We work with process pid= %i\n",getpid());
//exersise4
for (int i=0;i<4;i++){
sleep(1);
printf("i1= %i\n", i);
sleep(1);
fork();
sleep(3);
printf("pid= %i\n", getpid());
}
sleep(20);
return EXIT_SUCCESS;
}
b
# include <unistd.h>
# include <iostream>
# include <sys/types.h>
# include <sys/wait.h>
int main(void){
pid_t pid;
int status;
int rr=1;
printf("We work with process pid= %i\n",getpid());
//exersise5
for (int i=0;i<4;i++){
printf("i1= %i\n", i);
fork();
execl("/usr/bin/gnome-terminal", "gnome-terminal", "--", "/home/oksana/labOS/lab2021/childproc",
(char*)"5", (char*)0);
printf("pid= %i\n", getpid());
}
sleep(30);
return EXIT_SUCCESS;
}
c
# include <unistd.h>
# include <iostream>
# include <sys/types.h>
# include <sys/wait.h>
int main(void){
pid_t pid;
int status;
printf("We work with process pid= %i\n",getpid());
//exersise6
for (int i=0;i<4;i++){
printf("i1= %i\n", i);
sleep(1);
pid=fork();
if(pid==0){
printf("pid= %i\n", getpid());}
else {
sleep(5);
printf("We work in parent process. Child id: %i\n", pid);
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
}
}
return EXIT_SUCCESS;
}
//home_exersise2
int main(void){
pid_t pid;
int status;
printf("We work with process pid= %i\n",getpid());
for (int i=0;i<4;i++){
printf("i1= %i\n", i);
sleep(1);
pid=fork();
if(pid==0){
execl("/usr/bin/gnome-terminal", "gnome-terminal", "--", "/home/oksana/labOS/lab2021/childproc",
(char*)"5", (char*)0);
printf("pid= %i\n", getpid());}
else {
sleep(5);
printf("We work in parent process. Child id: %i\n", pid);
if (waitpid(pid, &status, 0) != -1) {
printf("Child exited with status %i\n", status);
} else {
perror("waitpid");
}
}
}
return EXIT_SUCCESS;
}
Download