Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW: Task 1234. #4

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
b61ad46
base ver. of tokenizator
Sep 14, 2022
1642f19
Delete a.out
shaazmik Sep 14, 2022
a8305ec
Delete task1/homework directory
shaazmik Sep 14, 2022
d75c0c2
Delete a.out
shaazmik Sep 14, 2022
89f8115
homework completed
Sep 20, 2022
cd397a9
Merge branch 'nefanov:main' into main
shaazmik Sep 20, 2022
4cf547e
fixed ARG_MAX
Sep 20, 2022
886819a
Merge branch 'nefanov:main' into main
shaazmik Sep 26, 2022
71aeec7
based architecture for program
Sep 26, 2022
1c7f49e
Merge branch 'main' of github.com:shaazmik/3_sem_22_23 into main
Sep 26, 2022
ea02414
completed
Sep 27, 2022
dc5d08d
cosmetics
Sep 27, 2022
671a1d5
deleted unused variables
Sep 29, 2022
eaf2acd
Merge branch 'nefanov:main' into main
shaazmik Oct 9, 2022
b8c5223
compeleted MQ and SM
Oct 13, 2022
95e56af
completed all programs, TODO start.py
Oct 15, 2022
3a1d646
Add files via upload
shaazmik Oct 18, 2022
ccbb771
Delete task3 directory
shaazmik Oct 18, 2022
0243614
start.py completed, TODO graphs
shaazmik Oct 18, 2022
b88a853
Delete fifo
shaazmik Oct 23, 2022
2755757
Delete output.txt
shaazmik Oct 23, 2022
b4e343b
get all data, TODO graphs
Oct 23, 2022
c5f1525
Create README.md
shaazmik Oct 23, 2022
274a730
Update README.md
shaazmik Oct 23, 2022
d48bba1
Update README.md
shaazmik Oct 23, 2022
90ead6b
Update README.md
shaazmik Oct 23, 2022
f0301f3
deleted stuff
Oct 23, 2022
4db9eb3
Merge branch 'main' of github.com:shaazmik/3_sem_22_23 into main
Oct 23, 2022
b884660
Graph completed
shaazmik Oct 25, 2022
37d4d3c
Delete img directory
shaazmik Oct 25, 2022
11115f8
Graph completed
shaazmik Oct 25, 2022
3994303
Completed
shaazmik Oct 25, 2022
fbb01f9
cosmetic
shaazmik Oct 25, 2022
117cb53
base functions
Oct 27, 2022
09eb6f8
Merge branch 'main' of github.com:shaazmik/3_sem_22_23 into main
Oct 27, 2022
b4329bc
refactoring
Oct 27, 2022
3488ae6
something intresting.....
Oct 27, 2022
90a8845
completed task, TODO graph
Oct 27, 2022
5e6bdd3
cosmetic
Oct 30, 2022
b509fd9
cosmetic#2
Oct 30, 2022
3a2b5c7
Add files via upload
shaazmik Oct 31, 2022
1a55d4b
Create README.md
shaazmik Oct 31, 2022
0e97318
Update README.md
shaazmik Oct 31, 2022
c425a75
Merge branch 'nefanov:main' into main
shaazmik Nov 21, 2022
405a1db
Merge branch 'nefanov:main' into main
shaazmik Nov 29, 2022
3786f9e
added 5
Nov 29, 2022
3671d77
added
Nov 29, 2022
7cfb6bc
get args func
Dec 3, 2022
7feb4bc
task6 smth went wrong....
Dec 5, 2022
9198936
task6 completed
Dec 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions task1/examples/10_exec_pipes.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,42 @@ void seq_pipe(char ***cmd)
int fd_in = 0;
int i = 0;

while (cmd[i] != NULL) {
while (cmd[i] != NULL)
{
pipe(p);

printf("New used pipe descriptors: %d %d\n",p[0],p[1]);
printf("Input descriptor for current child process: %d\n", fd_in);
if ((pid = fork()) == -1) {

if ((pid = fork()) == -1)
{
exit(1);
} else if (pid == 0) {
}
else if (pid == 0)
{
if (i > 0)
dup2(fd_in, 0); //stdin <- read from fd_in (dup / dup2(int oldfd, int newfd) newfd <--copy( oldfd ) )
dup2(fd_in, 0); //d) newfd <--copy( oldfd ) )

fprintf(stdout, "%d\n", fd_in);

if (cmd[i+1] != NULL)
dup2(p[1], 1); //stdout -> write to pipe

close(p[0]);

execvp((cmd)[i][0], cmd[i]);
exit(2);
} else {
}
else
{
wait(NULL);
close(p[1]);

if (i>0)
close(fd_in); // old pipe from previous step is not used further, and can be destroyed

fd_in = p[0]; //fd_in <--read from pipe
i++;
i++;
}
}
return;
Expand All @@ -56,8 +71,7 @@ int main()
{
char *ls[] = {"/bin/ls","-ltr",".", NULL};
char *grep1[] = {"grep","rw",NULL};
char *grep2[] = {"grep","1", NULL};
char **cmd[] = {ls, grep1, NULL};
seq_pipe(cmd); char *grep2[] = {"grep","1", NULL};
char *grep3[] = {"grep", "89", NULL};
char **cmd[] = {ls, grep1, grep2, grep3, NULL};
seq_pipe(cmd);
}
Binary file added task1/examples/a.out
Binary file not shown.
43 changes: 43 additions & 0 deletions task1/examples/beta.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Небольшое улучшение примера 10, которое позволяет держать не более 2 пайпов открытыми
Пайп пересоздается n-1 раз, но старые пайпы закрываются за ненадобностью. На одном пайпе я не вижу смысла делать, так как ввод и вывод перемешаются
Для того, чтобы убедиться, посмотрите вывод используемых дескрипторов (строка 22-23).
У меня это:
New used pipe descriptors: 3 4
Input descriptor for current child process: 0
New used pipe descriptors: 4 5
Input descriptor for current child process: 3
New used pipe descriptors: 3 5
Input descriptor for current child process: 4
New used pipe descriptors: 4 5
Input descriptor for current child process: 3
*/



#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

void seq_pipe(char ***cmd)
{
int pipe_fd[2] = {-1, -1};
pid_t pid ;
int fd_in = 0;
int i = 0;




return;
}

int main()
{
char *ls[] = {"/bin/ls","-ltr",".", NULL};
char *grep1[] = {"grep","rw",NULL};
char *grep2[] = {"grep","1", NULL};
char *grep3[] = {"grep", "89", NULL};
char **cmd[] = {ls, grep1, grep2, grep3, NULL};
seq_pipe(cmd);
}
31 changes: 31 additions & 0 deletions task1/homework/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "pcmd.h"
#include "tokenizator.h"

int main()
{
fprintf(stdout, "BASH:(0)_(0): ");

struct CmdTokens s_cmd = {};

if (constructTokens(&s_cmd) == CONSTRUCT_ERR)
{
fprintf(stderr, "ERROR: Unable to construct token structure\n");
destructEmptyTokens(&s_cmd);
return CONSTRUCT_ERR;
}

if (tokenizator(&s_cmd) == MAX_NUM_TOKENS_ERR)
{
fprintf(stderr, "ERROR: Maximum number of commands exceeded\n");
destructEmptyTokens(&s_cmd);
return MAX_NUM_TOKENS_ERR;
}

run(&s_cmd);

destructTokens(&s_cmd);

return 0;
}


4 changes: 4 additions & 0 deletions task1/homework/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CC=gcc

all:
gcc main.c pcmd.c tokenizator.c -D _DEBUG -ggdb3 -O0 -Wall -Wextra -Waggressive-loop-optimizations -Wmissing-declarations -Wcast-align -Wcast-qual -Wchar-subscripts -Wconversion -Wempty-body -Wfloat-equal -Wformat-nonliteral -Wformat-security -Wformat-signedness -Wformat=2 -Winline -Wlogical-op -Wopenmp-simd -Wpacked -Wpointer-arith -Winit-self -Wredundant-decls -Wshadow -Wsign-conversion -Wstrict-overflow=2 -Wsuggest-attribute=noreturn -Wsuggest-final-methods -Wsuggest-final-types -Wswitch-default -Wswitch-enum -Wsync-nand -Wundef -Wunreachable-code -Wunused -Wvariadic-macros -Wno-missing-field-initializers -Wno-narrowing -Wno-varargs -Wstack-protector -fcheck-new -fstack-protector -fstrict-overflow -flto-odr-type-merging -fno-omit-frame-pointer -pie -fPIE -fsanitize=address,alignment,bool,bounds,enum,float-cast-overflow,float-divide-by-zero,integer-divide-by-zero,leak,nonnull-attribute,null,object-size,return,returns-nonnull-attribute,shift,signed-integer-overflow,undefined,unreachable,vla-bound,vptr
52 changes: 52 additions & 0 deletions task1/homework/pcmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "pcmd.h"


void run(struct CmdTokens* s_commands)
{
// example /bin/ls -ltr . | grep rw

int pipeFd[2] = {-1, -1};
pid_t pid = -1;
int fd_in = -1;

for (size_t counter = 0; counter < s_commands->pipelineCapacity + 1; counter++)
{
pipe(pipeFd);

if ((pid = fork()) == -1)
{
fprintf(stderr, "ERROR:Unable to create process. A system-imposed limit on the number of threads was encountered\n");
}
else if (pid == 0)
{
if (counter > 0)
dup2(fd_in, 0); //stdin

if (counter != s_commands->pipelineCapacity) dup2(pipeFd[1], 1); //stdout

close(pipeFd[0]);

if (execvp(s_commands->flags[counter][0], s_commands->flags[counter]) == - 1)
{
fprintf(stderr, "ERROR: execvp error, check errno; Command not found.\n");
}

exit(-1);
}
else
{
int status = 0;

waitpid(pid, &status, WNOHANG);

s_commands->exitStatus[counter] = WEXITSTATUS(status);

close(pipeFd[1]);

if (counter > 0) close(fd_in);

fd_in = pipeFd[0];
}
}
return;
}
16 changes: 16 additions & 0 deletions task1/homework/pcmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PCMD_H
#define PCMD_H

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "tokenizator.h"

void run(struct CmdTokens* s_commads);



#endif
Loading