Commit efba7274 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

update lab1

parent 46ac4061
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
extern char **environ;
int main(int argc, char* argv[]) {
print_environment();
char *str = argv[1];
int pid1,pid2;
int *status;
if ((pid1 = fork()) == 0) {
execlp("/bin/cat", "cat", str, (char *) NULL);//
exit(0);
}
if ((pid = fork()) == 0) {
execlp("/usr/bin/wc", "wc", str, NULL);
exit(0);
}
else {
// parent process
wait(status);//
/*if (-1 != status){
if ((pid = fork())==0){
execlp("/usr/bin/wc", "wc", str, NULL);
}*/
//else{
// wait(status);
//}
exit(0);
}
}
void print_environment(void){
char **p;
for (p = environ; *p != NULL; p++){
char *element = *p;
printf("%s\n", element);
}
return 0;
}
CC = gcc
CFLAGS = -std=gnu99 -g #-DDEBUG
LFLAGS = -lpthread -lm
EXECS = cmdreverse cmds fork fork-data fork-test myprog fork-wait
all: $(EXECS)
cmdrevers: cmdreverse.c
$(CC) $(CFLAGS) -o $@ $<
fork-wait:fork-wait.c
$(CC) $(CFLAGS) -o $@ $<
fork: fork.c
$(CC) $(CFLAGS) -o $@ $<
cmds: cmds.c
$(CC) $(CFLAGS) -o $@ $<
fork-data: fork-data.c
$(CC) $(CFLAGS) -o $@ $<
fork-test: fork-test.c
$(CC) $(CFLAGS) -o $@ $<
myprog: myprog.c
$(CC) $(CFLAGS) -o $@ $<
.PHONY: clean
clean:
/bin/rm -f *.o $(EXECS)
(1.1)There are 2 num instances, one in the memory space of the parent and one in the memory space of the child. That is why the child increment by 1 while the parent increment by 3 at each time.
(1.1 b)There are 2 num instances, one in the memory space of the parent and one in the memory space of the child. That is why the child increment by 1 while the parent increment by 3 at each time.
"The child process is created with a single thread—the one that called fork(). The entire vir‐
tual address space of the parent is replicated in the child, including the states of mutexes,
condition variables, and other pthreads objects; the use of pthread_atfork(3) may be helpful for
dealing with problems that this can cause." from man 2 fork.
(1.2)
(2.1 a) i and x are in data segment. pid, j, and y are in stack segment.
(2.2.1 b) Yes, the datasegment and the stack segment of a parent process are copied over to the child process.
(2.2.2 b) No, the changes made by the child for data and stack segments will not be seen by the parent. Parent and child do not share
the same memory of data and stack, they have their own seperate segments.
(2.3 d) 1. Yes, the other can still read from it even one process closed the file.
2. Yes, after the child read the file, the parent will pick up where the child
left and starts reading.
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
extern char **environ;
int main(int argc, char* argv[]) {
print_environment();
char *str = argv[1];
int pid;
int *status;
if ((pid = fork()) == 0) {
execlp("/bin/cat", "cat", str, (char *) NULL);//
exit(0);
if ((pid = fork()) == 0) {
execlp("/usr/bin/wc", "wc", str, NULL);
exit(0);
}
else {
// parent process
wait(status);//
/*if (-1 != status){
if ((pid = fork())==0){
execlp("/usr/bin/wc", "wc", str, NULL);
}*/
//else{
// wait(status);
//}
exit(0);
}
}
void print_environment(void){
char **p;
for (p = environ; *p != NULL; p++){
char *element = *p;
printf("%s\n", element);
}
return 0;
}
File added
File added
/*
* CSCI 315 Operating Systems Design
* Date: 2014-09-02
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Revised by Xiannong Meng
* 2017-08-23
* added
* #include <unistd.h>
* #include <sys/types.h>
* #include <sys/wait.h>
*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int i = 7;
double x = 3.14;
int main(int argc, char* argv[]) {
int pid;
int j = 6;
double y = 2.18;
char buf_parent[6];
char buf_child[6];
int status; // exit status to be filled by wait
// create and open a file called data.txt
// use open (file descriptors)
// write into data.txt the following string:
// "this is a test for processes created with fork\nthis is another line"
// close the file so the writing can complete
// re-open the file for reading
if ((pid = fork()) == 0) {
// child process
// read 5 characters from file into buf_child
// print the characters in buf_child to terminal
printf("pid= %d -- initially, child sees x= %lf, y=%lf\n", pid, x, y);
x = 0;
y = 0;
printf("pid= %d -- child sees x= %lf, y=%lf\n", pid, x, y);
printf("child is terminating\n");
// close the file
exit(0);
} else {
// parent process
// read 5 characters from file into buf_parent
// print the characters in buf_parent to terminal
printf("pid= %d -- parent waits for child to terminate\n", pid);
printf("pid= %d -- before wait parent sees x= %lf, y=%lf\n", pid, x, y);
wait(&status); // note we are not catching the return value of wait!
printf("parent got termination status= %d from child\n", status);
printf("pid= %d -- after wait sees x= %lf, y=%lf\n", pid, x, y);
// read another 5 characters from file into buf_parent
// print the characters in buf_parent to terminal
// close the file
printf("parent is terminating\n");
exit(0);
}
}
......@@ -63,14 +63,15 @@ int main(int argc, char* argv[]) {
int numBytes_written = write(file_descriptor, a_str, sizeof(a_str));
close (file_descriptor);
file_descriptor = open("./data.txt", O_RDONLY);
if ((pid = fork()) == 0) {
//ERROR DETECTOR for the child
pid = Fork();
if (pid == 0) {
// child process
// read 5 characters from file into buf_child
// print the characters in buf_child to terminal
read(file_descriptor, buf_child, 5);
printf("%s\n", buf_child);
buf_child[5] = NULL;
printf("Child read: %s\n", buf_child);
printf("pid= %d -- initially, child sees x= %lf, y=%lf\n", pid, x, y);
x = 0;
......@@ -83,13 +84,15 @@ int main(int argc, char* argv[]) {
exit(0);
} else {
// parent process
// read 5 characters from file into buf_parent
// print the characters in buf_parent to terminal
read(file_descriptor, buf_parent, 5);
printf("%s\n", buf_parent);
buf_parent[5] = NULL;
printf("Parent read: %s\n", buf_parent);
printf("pid= %d -- parent waits for child to terminate\n", pid);
printf("pid= %d -- before wait parent sees x= %lf, y=%lf\n", pid, x, y);
......
/*
* CSCI 315 Operating Systems Design
* Date: 2014-09-02
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h> // need this for fork
#include <stdio.h> // need this for printf and fflush
int i = 7;
double x = 3.1415926;
int pid;
int main (int argc, char* argv[]) {
int j = 2;
double y = 0.12345;
if (pid = fork()) {
// parent code
printf("parent process -- pid= %d\n", pid); fflush(stdout);
printf("parent sees: i= %d, x= %lf\n", i, x); fflush(stdout);
printf("parent sees: j= %d, y= %lf\n", j, y); fflush(stdout);
} else {
// child code
printf("child process -- pid= %d\n", pid); fflush(stdout);
printf("child sees: i= %d, x= %lf\n", i, x); fflush(stdout);
printf("child sees: j= %d, y= %lf\n", j, y); fflush(stdout);
}
return(0);
}
#include <stdio.h>
/*
* CSCI 315 Operating Systems Design
* Date: 2014-09-02
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
int main(int argc, char *argv[]) {
int pid; int num;
if ((pid = fork()) == -1) {
perror("something went wrong in fork");
exit(-1);
} else if (pid == 0) {
for (num=0; num < 20; num++) {
printf("child: %d\n", num); fflush(stdout);
printf("Child Pid: %d\n", getpid());
sleep(1);
}
} else {
wait(NULL);
for (num=0; num < 20; num+=3) {
printf("parent: %d\n", num); fflush(stdout);
printf("Parent Pid: %d\n", pid);
sleep(1);
}
}
exit(0);
}
#include <stdio.h>
int main(int argc, char* argv[]) {
//int a = execlp("/bin/cat", "cat", "fork-file.c" , NULL);
//printf("!!!!!!!!!!!%d", a);
char p[] = "a"+"b";
//printf("%d", p);
printf("%s\n", p);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment