Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
ISC2
os
exercises
Commits
9364d082
Commit
9364d082
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
feat: ex1 pipes done
parent
65df02fc
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pipes/ex1/.gitignore
+2
-0
2 additions, 0 deletions
pipes/ex1/.gitignore
pipes/ex1/Makefile
+26
-0
26 additions, 0 deletions
pipes/ex1/Makefile
pipes/ex1/simple_pipe.c
+59
-0
59 additions, 0 deletions
pipes/ex1/simple_pipe.c
with
87 additions
and
0 deletions
pipes/ex1/.gitignore
0 → 100644
+
2
−
0
View file @
9364d082
*.o
simple_pipe
This diff is collapsed.
Click to expand it.
pipes/ex1/Makefile
0 → 100644
+
26
−
0
View file @
9364d082
CC
:=
clang
CFLAGS
:=
-g
-pedantic
-Wall
-Wextra
-std
=
c11
LDFLAGS
:=
-fsanitize
=
address
-fsanitize
=
leak
-fsanitize
=
undefined
-lm
-lpthread
TARGET
:=
simple_pipe
all
:
$(TARGET)
$(TARGET)
:
simple_pipe.o
@
printf
"=================== Building executable ===================
\n
"
$(
CC
)
$(
CFLAGS
)
$^
-o
$@
$(
LDFLAGS
)
@
printf
"
\n
"
%.o
:
%.c
@
printf
"================== Building object files ==================
\n
"
$(
CC
)
$(
CFLAGS
)
-c
$<
@
printf
"
\n
"
.PHONY
:
clean
clean
:
rm
-f
*
.o
$(
TARGET
)
.PHONY
:
rebuild
rebuild
:
clean all
This diff is collapsed.
Click to expand it.
pipes/ex1/simple_pipe.c
0 → 100644
+
59
−
0
View file @
9364d082
#include
<libgen.h>
#include
<stddef.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include
<time.h>
#include
<unistd.h>
#define DATA_LEN (1 << 9)
int
main
(
void
)
{
srand
(
time
(
NULL
));
int
arr_fd
[
2
]
=
{
0
};
char
buff
[
DATA_LEN
]
=
{
0
};
int
wstatus
;
for
(
size_t
i
=
0
;
i
<
DATA_LEN
;
i
++
)
{
buff
[
i
]
=
rand
()
%
INT8_MAX
;
fprintf
(
stdout
,
"%d "
,
buff
[
i
]);
}
fprintf
(
stdout
,
"
\n
"
);
if
(
pipe
(
arr_fd
)
==
-
1
)
{
perror
(
"pipe"
);
}
pid_t
pid
=
fork
();
if
(
pid
>
0
)
{
// Parent
// Parent closes exit FD
close
(
arr_fd
[
0
]);
write
(
arr_fd
[
1
],
buff
,
DATA_LEN
);
close
(
arr_fd
[
1
]);
wait
(
&
wstatus
);
if
(
WIFEXITED
(
wstatus
))
{
fprintf
(
stdout
,
"Child has terminated
\n
"
);
}
}
else
if
(
pid
==
0
)
{
// Child
// Child closes entry FD
close
(
arr_fd
[
1
]);
char
child_buff
[
DATA_LEN
]
=
{
0
};
size_t
count
=
0
;
size_t
bytes_read
=
0
;
while
((
bytes_read
=
read
(
arr_fd
[
0
],
child_buff
,
16
)))
{
count
+=
bytes_read
;
}
fprintf
(
stdout
,
"Number of bytes read: %lu
\n
"
,
count
);
}
else
{
// Error
perror
(
"fork"
);
}
return
EXIT_SUCCESS
;
}
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