$ echo 'abcd:bcde:cdeaf' | sed 's/^[^:]*://g'
bcde:cdeaf
first ^
означает начало строки. [^:]
- это единственный способ, которым я умею писать , а не двоеточие . *
после двоеточия означает любое количество вещей прямо передо мной (в данном случае не двоеточие). Наконец, :
выбирает двоеточие.
Другими словами, выберите начало строки, любое количество элементов, кроме двоеточия, и первое двоеточие.
// g
означает удаление каждого совпадающего экземпляра.
Debe usar sleep
para evitar que C se cierre inmediatamente. Pero en su estructura, tiene A esperando que C se cierre antes de generar B y D.
Entonces:
wait
para C en el mismo lugar que el bloque wait
para B waitpid
en lugar de wait
. Aquí está el código completo:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define SLEEP_TIME 5
int main() {
int status;
printf("I am: %d\n\n", (int)getpid());
pid_t c_pid = fork(); // fork a child
if(c_pid == 0)
{
printf("Hi I'm process C (%d) and my parent is %d\n",getpid(),getppid());
sleep(SLEEP_TIME);
exit(1);
}
else
{
pid_t b_pid = fork(); // fork a child
if (b_pid == 0)
{
printf("Hi I'm process B (%d) and my parent is %d.\n", getpid(), getppid());
pid_t d_pid = fork(); // fork a child
if (d_pid == 0)
{
printf("Hi I'm process D (%d) and my parent is %d.\n",getpid(),getppid());
sleep(SLEEP_TIME);
exit(3);
}
else
{
// sleep before wait - actually no effect as the wait for D also waits for SLEEP_TIME
sleep(SLEEP_TIME);
// Wait for D to quit
waitpid(d_pid, &status, 0);
int DReturnValue = WEXITSTATUS(status);
printf("parent knows child D (%d) finished with return value %d\n\n", (int) d_pid, DReturnValue);
}
exit(2);
}
else
{
sleep(SLEEP_TIME);
// Wait for B to quit
waitpid(b_pid, &status, 0);
int BReturnValue = WEXITSTATUS(status);
printf("parent knows child B (%d) finished with return value %d\n\n", (int) b_pid, BReturnValue);
// Wait for C to quit
waitpid(c_pid, &status, 0);
int CReturnValue = WEXITSTATUS(status);
printf("parent knows child C (%d) finished with return value %d\n\n", (int) c_pid, CReturnValue);
}
}
return 0;
}
Aquí está la salida correspondiente:
I am: 24450
Hi I'm process C (24451) and my parent is 24450
Hi I'm process B (24452) and my parent is 24450.
Hi I'm process D (24453) and my parent is 24452.
parent knows child D (24453) finished with return value 3
parent knows child B (24452) finished with return value 2
parent knows child C (24451) finished with return value 1