[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] C rutine til exec + pipe til og fra



> Jeg kan ikke umiddelbart reproducere det, så jeg husker nok forkert.
> 
> Men mht. delte af file descriptors over fork() vil de dog så vidt jeg
> ved dele alle flag undtagen close-on-exec flaget. Se fcntl(2): "The flags
> are shared between copies (made with dup(2), fork(2), etc.) of the same
> file descriptor)".
Du husker forkert, se kode nedenfor.

> Så skal du vel bare skrive
>   while (close(fd) && errno == EINTR);
Det er en legitim holdning.
 
> Præcis, nemlig i den klasse af fejl som brugeren aldrig opdager med
> mindre vi snakker om en long-running daemon. Derimod opdager brugeren med
> sikkerhed et uendeligt loop.
Vil brugeren ikke opdage en fejl, der er så alvorligt, at en fildeskriptor
ikke kan lukkes efter et par retries?

Hilsen, Hans-Christian Stadler

--- Kode ------------
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
        int fd[2], pid, nbytes, status;
        char buf[1024], *msg = "The message";
        time_t secs;
        
        if (pipe(fd))
                exit(-1);
        if ((pid = fork()) == 0) {
                // child
                secs = time(0);
                printf("%s: Start read\n", ctime(&secs));
                if ((nbytes = read(fd[0], buf, 1023)) < 0) {
                        if (errno == EAGAIN)
                                printf("Read returned EAGAIN\n");
                        exit(-2);
                }
                secs = time(0);
                buf[nbytes]=0;
                printf("%s: nbytes=%d, buf=%s\n", ctime(&secs), nbytes, buf);
                exit(0);
        }
        // parent
        if (pid < 0)
                exit(-3);
        if (fcntl(fd[1], F_SETFL, O_NONBLOCK))   // sætter dette O_NONBLOCK for
fd[0] i child processen også?
                exit(-4);
        sleep(10);
        secs = time(0);
        printf("%s: Start write\n", ctime(&secs));
        if (write(fd[1], msg, strlen(msg)) < 0)
                exit(-5);
        if (waitpid(pid, &status, 0) == -1)
                exit(-6);
        return WEXITSTATUS(status);
}

--- Output -------------------------------
Thu May 20 00:11:13 2004
: Start read               <-- blokerer 10 sekunder
Thu May 20 00:11:23 2004
: Start write
Thu May 20 00:11:23 2004
: nbytes=11, buf=The message



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *