Laat maar in NOS, ik kom te weinig in P&W om dit dan te lezen....
Op donderdag 03 januari 2002 09:25 schreef D2k het volgende:
pid=getpid(infp);
pid2=getpid(outfp);
[/code]
• pid=pid2=programma pid (klopt helemaal) maar ik wil de pid hebben van die pppd die ik gestart heb. Hoe kom ik daaraan?
Probleem is dat je d.n.v. popen() niet de pid
kan achterhalen, daarvoor moet je de applicatie zelf fork()en.... popen() is te simpel daarvoor, maar dat is dus ook de kracht van popen()
getpid() geeft de pid van je eigen applicatie, niet van de child die je met popen() geopent hebt. getpid() neemt ook geen argument aan (zie /usr/include/unistd.h)
Simpele manier om dit te doen:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| /* Code partly stolen from Linux Video Studio (http://ronald.bitfreak.net)
* Copyright (C) 2000-2001 Ronald Bultje
* All code is licensed under the GPL
*/
#include <stdio.h>
#include <errno.h>
int fd_p2c, fd_c2p; /* parent-to-child and child-to-parent */
int pid;
int active = 0; /* is the child active or not */
int kill_child()
{
if (active)
{
kill(pid, SIGINT);
waitpid(pid, NULL, 0);
active = 0;
return 0;
}
else
{
printf("Child is not active\n");
return -1;
}
}
int write_child(char *msg)
{
if (!active)
{
printf("Child is not active\n");
return -1;
}
if (write(fd_p2c, msg, strlen(msg)) != strlen(msg))
{
printf("write() failed: %s\n",
sys_errlist[errno]);
return -1;
}
return 0;
}
int read_child(char *buff)
{
char input[256];
int n;
if (!active)
{
printf("Child is not active\n");
return -1;
}
n = read(fd_c2p, input, sizeof(input)-1);
if (n<0) /* no output */
{
printf("read() failed: %s\n",
sys_errlist[errno]);
return -1;
}
else if (n==0) /* child is dead */
{
kill_child();
close(fd_p2c);
close(fd_c2p);
return -1;
}
else /* we have output */
{
strcpy(buff, input); /* make sure buff is big enough! */
return 0;
}
}
int create_child()
{
int ipipe[2], opipe[2];
char *command[3];
if (active)
{
printf("Close old child first!\n");
return -1;
}
command[0] = "pppd";
command[1] = "/dev/ttyS1";
command[2] = NULL;
if (pipe(ipipe) || pipe(opipe))
{
printf("pipe() failed: %s\n",
sys_errlist[errno]);
return -1;
}
if ((pid = fork()) < 0)
{
printf("fork() failed: %s\n",
sys_errlist[errno]);
return -1;
}
active = 1;
if (pid) /* parent */
{
fd_c2p = opipe[0]; /* to read from child */
fcntl(fd_c2p, F_SETFL, O_NONBLOCK);
close(opipe[1]);
fd_p2c = ipipe[1]; /* to write to child */
close(ipipe[0]);
}
else /* child */
{
close(ipipe[1]);
close(opipe[0]);
/* set stdin to our fd_p2c */
if (dup2(ipipe[0], 0) != 0)
exit(1);
close(ipipe[0]);
/* set stdout/stderr to fd_c2p */
if (dup2(opipe[1], 1) != 1)
exit(1);
if (dup2(opipe[1], 2) != 2)
exit(1);
close(opipe[1]);
/* execute command */
execvp(command[0], command);
exit(1);
}
return 0;
} |
Met Gtk (gdk_input_add()) kun je alle read() functies automatiseren met callbacks etc. KDE/Qt zal daar ook wel een functie voor hebben...
• i=pclose(infp); Dit zou de return waarde moeten afvangen maar deze is altijd 0 ?? Het uitvoeren enz lukt gewoon
pclose() geeft 0 als return value omdat dat gelukt is, denk ik