Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a comprension question about this code that I am currently studying: this function is used in a C Shell implementation to execute piped commands. I can't understand how the person who wrote it got to know how many pipes to close (why is the limit 2*com- 2)?
for(i = 0; i < 2*com - 2; i++) close(pip[i]);
for(i = 0; i < com; ++i) {
waitpid(pid, &status, WUNTRACED);
In this program, num_pipe is not actually the number of pipes
but the number of commands (very bad name indeed!).
Between two commands you need one pipe, between three commands
you need two pipes ... between N commands you need N-1 pipes.
Each pipe relies on two file descriptors (one for reading, one
for writing) thus 2*(num_pipe-1) file descriptors are needed
for num_pipe commands.
note: the malloc() does not allocate an array of integer pointers
(as stated in the question) but an array of integers.
Following this logic, I would have written
for(i = 0; i < 2*(num_pipe-1); i += 2)
but 2*(num_pipe-1) equals to 2*num_pipe-2 and since the step
is 2, the loop condition is the same with the limit
2*num_pipe-3.
It's just terribly confusing in my opinion.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm new to C programming, so I wonder if it's possible to read from a certain file using the system call read() until a space is found.
Example:
A file contains a number and a process PID (59 5542). I want to read first the number, saving it into a variable and then do the same thing with the PID.
Thanks in advance.
P.S: since this is an exercise for my Operating Systems class, I have to use read().
I recommend first reading the number and the PID with one read() call into a sufficiently large buffer, then saving both into variables, e. g.:
char buf[20] = { 0 }, *end;
read(fd, buf, sizeof buf - 1);
int num, PID;
num = strtol(buf, &end, 0);
PID = strtol(end, NULL, 0);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I just learned about fork on the web, I understand the main principle with the child and parent processes but I am still a little bit confused on how we can use fork to search faster through a 3D array. Can anyone give a quick coded example to show how it works ?
Thanks
Fork can make things run faster by allowing the calculations to be split up amongst the processors. Here's example code using a flat array (it's easier to get the concept across with a flat array instead of 3d array):
int main() {
int i;
int array[] = {0,1,2,3,4,5,6,7,8,9,10};
int findThisNumber = 8;
int pid = fork(); //split into two processes
//the parent return the childs pid
//and the child returns 0
if(pid == 0) { //this is the child
for(i = 0; i < 5; i++) { //scan the first half of the array
if(array[i] == findThisNumber) {
printf("%d found at index %d\n", findThisNumber, i);
}
}
} else { //this is the parent
for(i = 6; i < 10; i++) { //scan the second half
if(array[i] == findThisNumber) {
printf("%d found at index %d\n", findThisNumber, i);
}
}
}
}
In this example, the program splits into two processes and each process searches half of the array. I ran a the same program with 1000000000 elements in the array and these are the times:
time ./noFork.exe
real 0m0.796s
time ./a.exe
real 0m0.391s
I hope that helps, if I can clear anything else up let me know.
I recommend you to check out posix threads. The difference is that threads work in the same process so they share address space (imo it is faster and easier to exchange data between threads then between processes). To search faster you should divide N dimensional array to X groups(smaller arrays - one for each thread/process) and pass each group of N dimensional data to particular thread(pthread)/process(fork).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am wondering if it is possible to run same program twice, and every time it does something different. For instance I have two programs one that writes with fifo pipes and one that reads from it. So there are programA.c and programB.c (simple program, just sending some integers).
But I would like to run it like that:
./program & sleep 1; ./program
So one program would have two operation modes.
Thanks.
Yes, it is possible. You can run a program however many times you want. However, you may need to ensure that two instances of the same program do not contend for the same resources; for instance, if they both write to the same file, unexpected results may occur.
If you want the same program to do two different things (one writing to the fifo and one reading from it) you will have to ensure that the program can determine which action to take. One way of doing this would be to parse command-line parameters (e.g. invoke one as myprog --read and the other as myprog --write. Another would be for the program first to check for the existence of the fifo; if it doesn't exist, it could create the fifo and write to it, and if it does exist it could read from it.
Write your program to accept command line arguments like sed or awk.
Here is a simple example in C:
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 1 ) printf("You passed in zero arguments and your program is named %s\n", argv[0]);
if( argc > 1 ){
int x;
for(x = 1; x < argc; x++)
printf("Argument %d is named %s\n",x, argv[x]); //print multiple arguments
}
return 0;
}
If you compile and run this program with no arguments it will tell you that you didn't pass in any arguments; otherwise, you can pass in as many command line arguments as you like and it will print them back out for you.
The point is that this one program does different things depending on the arguments passed into it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have to create a wordcount program in C using Posix. I also have to use multithreading using pthreads by reading the input file into a buffer and then paritioning that buffer given how many threads are going to be used. Then, each thread should count the number of words in its partition. The problem is i cant find any sources at all for splitting or partitioning the contents in a buffer. Any help at all would be appreciated.
Assuming your program stores the file as a pointer to an array of characters, you might achieve a similar result by representing a "partition" of the buffer as a pointer to the start of your partition and an integer to represent the size of the partition.
The code snippet below may help you with partitioning the buffer.
struct buffer_partition {
char* start;
int size;
}
void* word_count(void* arg) {
struct buffer_partition* buffer=(struct buffer_partition*)arg;
/* do word counting with buffer->start
be careful to not access characters after buffer->size */
}
int main() {
/* read input */
struct buffer_partition* partition = (struct buffer_partition*)malloc(sizeof(struct buffer_partition));
partition->start=buffer+offset;
partition->size=size_of_each_partition;
pthread_create(thread_pointer, extra_attributes, word_count, partition);
/* sum all results, print answer :) */
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have C binary program that receives one-word input. I have a file which contains a list of the words to be inputted to the program, one at a time.
so my script looks something like this"
while read line
do
./program ${line}
done < myfile"
If I replace ./program with echo, every argument is printed properly. However, when I input it into the program via alias ($line), program receives blank. Please explain how to fix it and why this is happening.
make a debug program to see what is going on...
int main (int argc, const char * argv[])
{
printf("\n");
// insert code here...
for(int i = 0; i< argc ; i++)
{
printf("argc == %i, argv[%i]==%s\n",argc,i,argv[i]);
}
return 0;
}