Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
C-Stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marc Feger
C-Stuff
Commits
1659e22a
Commit
1659e22a
authored
7 years ago
by
Marc Feger
Browse files
Options
Downloads
Patches
Plain Diff
Fork and Pipe QSum example
parent
9cd458dc
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Fork_and_Pipe/pipeline_qsum.c
+43
-0
43 additions, 0 deletions
Fork_and_Pipe/pipeline_qsum.c
with
43 additions
and
0 deletions
Fork_and_Pipe/pipeline_qsum.c
0 → 100644
+
43
−
0
View file @
1659e22a
#include
<unistd.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<sys/wait.h>
#include
<string.h>
int
main
(
int
argc
,
char
*
argv
[]){
pid_t
pid
;
char
data
[
80
];
int
rb
;
int
pipe_ends
[
2
];
// 0 read, 1 write
int
status
=
1
;
pipe
(
pipe_ends
);
pid
=
fork
();
if
(
pid
==
(
pid_t
)
0
){
printf
(
"Child: %d
\n
"
,
getpid
());
int
qsum
=
0
;
close
(
pipe_ends
[
0
]);
// want to write
for
(
int
i
=
0
;
i
<
strlen
(
argv
[
1
]);
i
++
){
qsum
+=
(
int
)(
argv
[
1
][
i
]
-
'0'
);
}
sprintf
(
data
,
"%d"
,
qsum
);
write
(
pipe_ends
[
1
],
data
,
strlen
(
data
));
close
(
pipe_ends
[
1
]);
sleep
(
5
);
exit
(
0
);
}
else
{
printf
(
"Parent: %d
\n
"
,
getpid
());
waitpid
(
pid
,
&
status
,
0
);
close
(
pipe_ends
[
1
]);
int
rd
=
read
(
pipe_ends
[
0
],
data
,
79
);
data
[
rd
]
=
'\0'
;
close
(
pipe_ends
[
0
]);
printf
(
"Done with status: %d
\n
qsum: %s
\n
"
,
status
,
data
);
}
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment