prints.h
调试宏
#ifndef CXXU
#define CXXU 1
#define sizeint__ "25"
#define sizestr__ "%" sizeint__ "s"
#define dprint(expr) printf(sizestr__ " = %d @%%d\n", #expr, expr)
#define ldprint(expr) printf(sizestr__ " = %ld @%%ld\n", #expr, expr)
#define cprint(expr) printf(sizestr__ " = %c @%%c\n", #expr, expr)
#define sprint(expr) printf(sizestr__ " = %s @%%s\n", #expr, expr)
#define gprint(expr) printf(sizestr__ " = %g\n", #expr, expr)
#define fprint(expr) printf(sizestr__ " = %f\n", #expr, expr)
#define sprintln(expr) printf(expr "\n")
#define pre_print(expr) printf(expr)
#define pprint(expr) printf(sizestr__ " = %p &var%%p\n", "&" #expr, &expr)
#define pprinta(expr) printf(sizestr__ " = %p %%p (pointer:" #expr ")\n", #expr, expr)
#endif
多重fork
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "prints.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "common_fun.c"
int counter = 0;
int main(int argc, char const *argv[])
{
pid_t fp1;
pid_t fp2;
sprintln("---before fork---,this line should just be print once");
sprintln("~~~fork p1");
fp1 = fork();
dprint(fp1);
if (fp1 == 0)
{
printf("\t");
sprintln("I am child process p1");
dprint(getpid());
dprint(getppid());
pid_t fp3 = fork();
if (fp3 == 0)
{
pre_print("\t\t");
sprintln("I am child process p3");
dprint(getpid());
dprint(getppid());
}
else{
sleep(1);
}
}
else
{
sprintln("I am main process");
dprint(getpid());
sprintln("~~~fork p2---");
fp2 = fork();
if (fp2 == 0)
{
printf("\t");
sprintln("I am child prcess p2");
dprint(getpid());
dprint(getppid());
}
sleep(1);
}
return 0;
}
|