P09 lab code added
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
//***************************************************************************
|
||||
// File: ChildProcA8.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>
|
||||
#include <signal.h>
|
||||
|
||||
//***************************************************************************
|
||||
// Function: main(), parameter: arg[0]: Programmname, arg[1]: i
|
||||
//***************************************************************************
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int i = 0, *a = NULL;
|
||||
|
||||
if (argc > 1)
|
||||
i = atoi(argv[1]); // convert string argv[1] to integer i
|
||||
// argv[1] is a number passed to child
|
||||
|
||||
printf("\n*** I am the child having job nr. %d ***\n\n", i);
|
||||
|
||||
switch(i) {
|
||||
case 0: exit(0); // exit normally
|
||||
break;
|
||||
case 1: *a = i; // force segmentation error
|
||||
break;
|
||||
case 2: kill(getpid(), 30); // I send signal 30 to myself
|
||||
break;
|
||||
case 3: sleep(5); // sleep and wait for signal
|
||||
break;
|
||||
case 4: sleep(5); // just sleep
|
||||
exit(222); // then exit
|
||||
break;
|
||||
default:
|
||||
exit(-1);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
@@ -0,0 +1,65 @@
|
||||
//***************************************************************************
|
||||
// File: ProcA8.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(int argc, char *argv[]) {
|
||||
pid_t pid;
|
||||
int status, retval, whatToDo = 0;
|
||||
char str[8];
|
||||
|
||||
if (argc > 1)
|
||||
whatToDo = atoi(argv[1]); // get job number for child
|
||||
|
||||
pid = fork(); // fork child
|
||||
switch (pid) {
|
||||
case -1:
|
||||
perror("Could not fork");
|
||||
break;
|
||||
case 0:
|
||||
sprintf(str, "%d",whatToDo);
|
||||
retval = execl("./ChildProcA7.e", "ChildProcA7.e", str, NULL);
|
||||
if (retval < 0) perror("\nexecl not successful");
|
||||
break;
|
||||
default:
|
||||
if (whatToDo <= 3) {
|
||||
if (whatToDo == 3) {
|
||||
sleep(1);
|
||||
kill(pid, SIGABRT); // send signal SIGABTR to child
|
||||
}
|
||||
wait(&status);
|
||||
if (WIFEXITED(status))
|
||||
printf("Child exits with status %d\n", WEXITSTATUS(status));
|
||||
if (WIFSIGNALED(status)) {
|
||||
printf("Child exits on signal %d\n", WTERMSIG(status));
|
||||
printf("Child exits with core dump %d\n", WCOREDUMP(status));
|
||||
}
|
||||
} else {
|
||||
usleep(500*1000);
|
||||
while (!waitpid(pid, &status, WNOHANG)) {
|
||||
printf(". . . child is playing\n");
|
||||
usleep(500*1000);
|
||||
}
|
||||
printf("Child has exited with 'exit(%d)'\n", WEXITSTATUS(status));
|
||||
}
|
||||
break;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
Enable core dumps -> ulimit -c unlimited
|
||||
Disable core dumps -> ulimit -c 0
|
||||
|
||||
Das core File lesen mit gdb:
|
||||
|
||||
gdb -c core ChildProgA8.e
|
||||
|
||||
|
||||
!!!! Wichtig !!!!
|
||||
|
||||
Wenn sie ein Linux benutzen, das standardmaessig ABRT (Automatic Bug
|
||||
Reporting Tool) verwendet (z.B. Fedora, Centos, etc.), muss
|
||||
/proc/sys/kernel/core_pattern auf "core" gesetzt werden, damit ein
|
||||
core File erzeugt wird
|
||||
|
||||
echo core > /proc/sys/kernel/core_pattern
|
||||
|
||||
Wenn ihr System SE-Linux verwendet (z.B. Fedora) , laesst sich core_pattern
|
||||
nicht auf einfache Art setzen, verzichten sie in diesem Fall auf die
|
||||
Erzeugung des "core" Files.
|
||||
@@ -0,0 +1,34 @@
|
||||
# *************************************************************
|
||||
# Original Autor: M. Thaler (Modul BSY)
|
||||
# *************************************************************
|
||||
|
||||
CMP= gcc
|
||||
CMPFLAGS= -Wall -g
|
||||
LDFLAGS=
|
||||
EXENAME= ProcA7.e
|
||||
FNAME= ProcA7
|
||||
EXENAMC= ChildProcA7.e
|
||||
FNAMC= ChildProcA7
|
||||
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 core *.o $(EXENAME) $(EXENAMC)
|
||||
|
||||
all:
|
||||
@make clean
|
||||
@make
|
||||
Reference in New Issue
Block a user