P09 lab code added
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//***************************************************************************
|
||||
// File: ChildProcA3.c
|
||||
// Original Author: M. Thaler (Modul BSY)
|
||||
//***************************************************************************
|
||||
|
||||
//***************************************************************************
|
||||
// system includes
|
||||
//***************************************************************************
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
//***************************************************************************
|
||||
// Function: main(), parameter: arg[0]: Programmname, arg[1]: i
|
||||
//***************************************************************************
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int i;
|
||||
|
||||
if (argv[1] == NULL) {
|
||||
printf("argument missing\n");
|
||||
exit(-1);
|
||||
}
|
||||
else
|
||||
i = atoi(argv[1]); // convert string argv[1] to integer i
|
||||
// argv[1] is a number passed to child
|
||||
|
||||
printf("\n... ich bin das Kind %d mit i=%d, ", getpid(),i);
|
||||
printf("meine Eltern sind %d \n", getppid());
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
@@ -0,0 +1,54 @@
|
||||
//***************************************************************************
|
||||
// File: ProcA3.c
|
||||
// Original Author: M. Thaler (Modul BSY)
|
||||
//***************************************************************************
|
||||
|
||||
//***************************************************************************
|
||||
// system includes
|
||||
//***************************************************************************
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
//***************************************************************************
|
||||
// Function: main(), parameter: none
|
||||
//***************************************************************************
|
||||
|
||||
int main(void) {
|
||||
|
||||
pid_t pid;
|
||||
int status;
|
||||
int i, retval;
|
||||
char str[8];
|
||||
|
||||
i = 5;
|
||||
|
||||
printf("\n\ni vor fork: %d\n\n", i);
|
||||
|
||||
pid = fork();
|
||||
switch (pid) {
|
||||
case -1:
|
||||
perror("Could not fork");
|
||||
break;
|
||||
case 0:
|
||||
i++;
|
||||
sprintf(str, "%d",i); // convert integer i to string str
|
||||
retval = execl("./ChildProcA2.e", "ChildProcA2.e", str, NULL);
|
||||
if (retval < 0) perror("\nexecl not successful");
|
||||
break;
|
||||
default:
|
||||
i--;
|
||||
printf("\n... wir sind die Eltern %d mit i=%d ", getpid(), i);
|
||||
printf("und Kind %d,\n unsere Eltern sind %d\n", pid, getppid());
|
||||
wait(&status);
|
||||
break;
|
||||
}
|
||||
printf("\n. . . . . und wer bin ich ?\n\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
@@ -0,0 +1,34 @@
|
||||
# *************************************************************
|
||||
# Original Autor: M. Thaler (Modul BSY)
|
||||
# *************************************************************
|
||||
|
||||
CMP= gcc
|
||||
CMPFLAGS= -Wall
|
||||
LDFLAGS=
|
||||
EXENAME= ProcA2.e
|
||||
FNAME= ProcA2
|
||||
EXENAMC= ChildProcA2.e
|
||||
FNAMC= ChildProcA2
|
||||
LIBNAME=
|
||||
LIBNAME=
|
||||
|
||||
compile: $(EXENAME) $(EXENAMC)
|
||||
|
||||
$(EXENAME): $(FNAME).o
|
||||
$(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@
|
||||
|
||||
$(EXENAMC): $(FNAMC).o
|
||||
$(CMP) $(FNAMC).o $(LIBNAME) $(LDFLAGS) -o $@
|
||||
|
||||
.c.o:
|
||||
$(CMP) -c $(CMPFLAGS) $<
|
||||
|
||||
.cc.o:
|
||||
$(CMP) -c $(CMPFLAGS) $<
|
||||
|
||||
clean:
|
||||
rm -f *.o $(EXENAME) $(EXENAMC)
|
||||
|
||||
all:
|
||||
@make clean
|
||||
@make
|
||||
Reference in New Issue
Block a user