bash and c - 2 processes in concurrency to write a file
Hi,
I made a C program, it forks 2 processes and each one of them uses Code:
system() For now my script1.sh (from process P1) write in the file for first, and then script2.sh (from process P2) write in the same file, but I need them to write in this way: Code:
time 1: P1 writes 2 words in "output.txt" Code:
system() Code:
system("script1.sh &") I also know how to stop P1 using Code:
pkill -f "script1.sh How can I reach that aim? Thanks edited at the bottom: |
Try this:
script1: Code:
while true; do Code:
while true; do |
ok and from the c programm do I have to call this 2 scripts with system("name.sh") or system("name.sh &")?
The problem is that if I call these 2 scripts with Code:
system("script1.sh &") Code:
system("script1.sh &") Code:
printf("\nSCRIPT called by child1:\n"); |
I will explain it better:
This is what I need: I made a C program, it forks 2 childs: P1, P2 I also made some bash scripts. I need P1 to run script1.sh, and P2 to run script2.sh. At the moment I'm using the function Code:
system("script_name.sh") Code:
system("script_name &") script1.sh does this: Code:
# it search a word in a dictionary and write in a file the exact line in which that Code:
# delete lines starting with letter 'z' FROM SAME FILE THAT USES script1.sh How can I do this from C program? The important thing is that every process runs one script, and these scripts has to be executed in parallel alternating them. In this way I reach my aim that is to let 2 processes to produce one output together in order to allowa one third process to read that output and work on it! I hope it's clear Thanks for your help |
Your code examples are way too long for me to even bother to read, so I'll rephrase the problem hoping that that's what you need.
Having a file “input.txt” with the following content: Code:
line 1 Code:
#!/bin/sh Code:
#!/bin/sh Code:
script-1 says: line 1
If, on the other hand, your problem is that having a script “script-1.sh” with the following content: Code:
#!/bin/sh Code:
#!/bin/sh Code:
script-1 says 1
|
that's great and I think that it's what I need, the problem is that I don't know those function and I don't know how to put them together :/
For example I don't know about sharing pipes from bash to C or from C to bash (maybe I did'nt understand your answer). Btw thanks because that's what I need to do. Maybe I could give you my code to understand how to do those steps on my code: Code:
switch(child1=fork()) { |
Quote:
Quote:
|
thanks for your replies, the problem is that understanding this from zero is very difficult for me and I'm not english :/
Could you please make an example on my code just to understand something. the are also my scripts up here in order to see what they do. Thanks |
pipe man page has a nice example of how to use pipe, so for starters compile it and start playing with it.
First, change it so that child uses execl() to call the script instead of doing anything by itself. Example usage may be found in execve man page. For your use case, it seems that you'll be interested in calling execl("./script1.sh", "./script1.sh", (char *)0);. Second, change it so that instead of writing argument to the pipe it copies standard input to the process. With that, you can further change it so that it prints some message after writing each line. If you need to read output from the script and not write to it, than reverse the direction. With that, it should be not that hard to create another child and write/read data to/from one and than the other. |
I have read this thread several times, but I'm still confused.
1) What are you trying to do in the first place? 2) Why do you need one C program and two shell scripts to do it? If I understand it correctly (which I most likely don't), you have one input file and two scripts, such that Code:
[ INPUTFILE ] ----> [ script1 ( LOOP ) ] ----> [ OUTPUTFILE1 ] Code:
,--> [ script1 ( LOOP ) ] -. Code:
,--> [ script1 ( one line per call ) ] -. Also, reading your code from the post #4, please note that you cannot have one process write into a file and sed -i with it in the same time and expect to get anything reasonable. |
thanks to everybody, I'll work on that.
|
All times are GMT -5. The time now is 11:20 PM. |