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 days ago.
Improve this question
mytime.x: run a command and track its running time. Syntax: mytime.x cmd [arguments], where cmd is the command to run, and the arguments are the optional command line arguments to the command cmd. The output of mytime.x should be similar to the Unix command time. At the minimum, mytime.x should report three values: 1) user CPU time; 2) system CPU time; and 3) elapsed wall-clock time for running the command cmd. There are different ways to implement this utility. As a starting point, you can read the manual pages of the Unix system calls wait, waitpid, wait3, wait4, getrusage, and times. You can assume that the "cmd" command-line argument of mytime.x is in one of the search paths included in the PATH environment variable in C language without using the strftime.
Tried but couldn't able to do it.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I need to write a C program that launches another program with a modified priority, much as as the nice command does. In order to do that, I would like to find the PID of a process given as an argument (how can I do that?) and modify its priority level (how can I do that?).
Example: The command line might be ./a.out 5 sleep 500 &, and this should produce the same effect as nice -n 5 sleep 500&.
You are focusing on the wrong thing and therefore approaching the problem with the wrong idea. The key requirement is that your program must execute a specified command. Focusing on that will lead you toward how to achieve the process priority goal, at least by helping you frame the question more usefully. For example, you don't need to find any PID, because you don't need to adjust the niceness of an arbitrary process.
So how do you programmatically launch another command? The typical way would be to use one of the functions from the exec family. Since the program name and arguments are comming from the command line, execvp() is probably your best choice.
If you read their docs, you will find that the exec functions replace the process image in the current process. That is, they make the process in which they are called start and run a different program in place of the one it was running before. If the command you're going to launch will run in the current process, then it's the current process whose niceness you want to adjust, and for that there is nice().
You shouldn't need much more than those two functions and a little command-line parsing. Do read those functions' documentation carefully, however, especially execvp()'s, to make sure you set up the arguments correctly.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I need to play a wave file using aplay. However, my wave files are few seconds in length. So I plan to play them in loop for certain period of time. aplay does not seem to have loop options. From my C file create a sub-process using popen() The playback is working fine. But the difficulty is that I can't monitor when the wave file playback is completed so that I can start over.
popen("aplay WaveFile","r");
How do I know when this command has finished playback?
Thanks in advance!
From the popen manpage:
The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2).
If you don't want to be blocked on pclose, you may try non-blocking reads on the pipe returned by popen, until you get an EOF which means the process has terminated.
You can look for various option of aplay from the man page here
one of the option is -d which is
-d, --duration=#
Interrupt after # seconds. A value of zero means infinity. The default is zero, so if this option is omitted then the arecord process will run until it is killed.
So if you know the wave file play duration you can give that number with -d.
There are many ways to calculate the wave file length like here
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've looked into the Linux kernel source and I was wondering what the kernel sees as a task? Because obviously the CPU runs machine instructions so I thought the scheduler fetches maybe the memory adress of the main function of a program and puts it on the CPU. Is that at least kind of correct? When I click on an executable program, what will actually happen inside the scheduler?
EDIT:
I saw several task-related structs in the source code that stored a bunch of integers and floats (flags, priority etc.)...But I am wondering how the scheduler finds the machine instructions of my programs.
At a minimum a task is a set of register values. One of them is the program counter. When the kernel switches task it stores the current values of all registers in the task structure for the old task. It then loads all the register values from the new task structure, loading the program counter last. This resumes the execution of that task.
Now the hard to understand part is that in most kernels the program counter isn't loaded at all at task switch. Now how can that switch tasks then?
The trick is that all task switching is always done in the same function, which must be written in ASM. So the program counter of the old task is always exactly the program counter of the new task. So it doesn't need to be loaded at all. Execution simply continues where it is. BUT the code now runs in the context of the new task. And when the function returns it returns from where previously the new task called the task switching function. Maybe it's simpler to understand if I say that the new tasks program counter is loaded from the stack when the task switch function returns.
Anyway, what the scheduler does is switch the whole CPU state from one task to the other. It's much more than just a function pointer in C. If you want a C equivalent then look at setjmp() + longjmp().
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
How can I determine if a command is successful or exists in C? This will need to be compatible with multiple architectures and routers (don't ask haha)
I was thinking of using popen or system or exec(v)(l). But the command I want to check for is sendmail. Since sendmail is a command that runs forever with no output this will be a little hard to do. Any ideas?
Thanks in advance.
If you know the path to sendmail, then as per the comments, use stat to check if it exists and is executable. If you know the search path then iterate through it and check each one.
If not - you could pass it /dev/null as stdin, in which case it will print an error message and exit. Then you can inspect the exit code from the process - for POSIX systems, the exit code will be 127 if the command could not be executed:
int rc = system("sendmail </dev/null 2>/dev/null");
if (WEXITSTATUS(rc)!=127) {
/* sendmail was found */
}
Of course this requires /dev/null to exist and shell redirects to be available.
I redirected stderr also, otherwise you will get error messages output from sendmail.
Just try to use it. If it's there, and you can execute it, it'll work. If it isn't, it won't and you can see why in errno.
The reason you don't try to check beforehand is twofold, and it's the same with files as with executables. First, you might get the check wrong. A lot of people will make the mistake of checking if a file exists and then wonder why opening it fails. They forgot to check if it's readable. There's no need to duplicate all these checks, just try to open it and see.
The second is it invites a race condition. For example, let's say you have two processes working on the same file. Time is moving down.
Process 1 Process 2
Check if file is readable.
It's readable!
Make the file unreadable.
Open that file.
File fails to open.
That's a very simple example, there are far, far worse consequences to a race condition. It illustrates that checking if a resource can be used and using that resource must be atomic: it has to happen in one single uninterruptible operation.
Avoid popen and system where you can. They run the command through the shell which invites security holes and unintended consequences of non-alphanumeric characters. Instead, use one of the exec[lv]p functions that will search the PATH for the executable without invoking a shell.
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 created 6 Sockets and simultaneously listening to all of them using select. I want to find out how much time does the CPU take switching from 1 socket to another. Does anyone know; if not can someone guide me please on how to compute this problem!
I think you may have misunderstood what the select call is actually doing, the man page for select says the following:
Three independent sets of file descriptors are watched. Those
listed in readfds will be watched to see if characters become
available for reading (more precisely, to see if a read will not
block; in particu- lar, a file descriptor is also ready on
end-of-file), those in writefds will be watched to see if a
write will not block, and those in exceptfds will be watched for
exceptions. On exit, the sets are modified in place to indicate
which file descriptors actually changed status. Each of the three
file descriptor sets may be specified as NULL if no file descriptors
are to be watched for the corresponding class of events.
So when your call to select returns what it will tell you is which, if any, of the file descriptors are (in your case) ready to be read from. It's then up to you to decide which to read and how to read it.
If you can I'd reccomend tracking down a copy of Unix Network Programming (by Stevens, Fenner, Rudoff). This will give you all the background information and example C code that you will ever want on network programming.
Or look at the tutorial here