C error - wait() - c

I write a program using memory segments but the problem is my wait() is throwing me an error
error:
ks.c:24:2: error: incompatible type for argument 1 of ‘wait’
/usr/include/x86_64-linux-gnu/sys/wait.h:116:16: note: expected ‘__WAIT_STATUS’ but argument is of type ‘int’
s.c:13:6: warning: unused variable ‘w’ [-Wunused-variable]
Code line is simple wait(1); and i have includeted all neccesarry ehaders for the project why do i get that error, since it should work like this...
imports
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <errno.h>
#include "st.h"

If you check the documentation for wait, what it takes as parameter is a pointer to int where to set the status. Since this is homework, I'll let you conclude the solution yourself.

If you look at the manual page for wait it requires a pointer to an integer.
So your code should read something like
int status;
wait(&status);

The wait function actually take a pointer to an int and returns the exit status of the thread's child in that pointer.
Try something like this
int status;
wait(&status);

Read the error message thoroughly. It says incompatible type for argument 1 of ‘wait’ and then note: expected ‘__WAIT_STATUS’ but argument is of type ‘int’. You can then have a look in the manual $ man wait where everything's very shortly explained - argument and return types, typical values, etc. You might even get lucky and find an example without even searching the net.
If you were missing a .header file, you would have gotten a different error message

Related

error: implicit declaration of function 'sysconf'

Following this question, I'm trying to use sysconf to get the number of processors on a Linux machine:
#include <unistd.h>
int main()
{
...
int CPUs = sysconf(_SC_NPROCESSORS_ONLN);
...
}
However, the compiler gives me this error:
error: implicit declaration of function 'sysconf'
Am I doing something wrong? I tried to also add #include <sys/sysinfo.h> but nothing changed.

How to reproduce "2 No such file or directory" in C

I know it might sound strange but I'm trying to find when to get this type of error when passing a wrong argument to a main program.
Let's say I have a program that accepts 1 or 2 arguments. If it's 2 arguments it can only be:
argv[0] =./programName
and
argv[1] = "-A".
Any other argv[1] other than "-A" needs to printf a "2 No such file or directory" message.
As far as I know, this is a system message, so printing it will not work for me.
DO I need to save all possible main arguments in a file and then compare the typed arguments with the ones in the file?
Right now the way I have it is:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/wait.h>
int main (int argc, char *argv[]){
.....
...
if (argc == 2 && strcmp(argv[1], ARGV_2)!=0){
return(EXIT_FAILURE);
}
...
.....
}
I think ls does this with the error() function:
GNU Error_messages
Function: void error (int status, int errnum, const char *format, …)
Preliminary: | MT-Safe locale | AS-Unsafe corrupt heap i18n | AC-Safe
| See POSIX Safety Concepts.
The error function can be used to report general problems during
program execution. The format argument is a format string just like
those given to the printf family of functions. The arguments required
for the format can follow the format parameter. Just like perror,
error also can report an error code in textual form. But unlike perror
the error value is explicitly passed to the function in the errnum
parameter. This eliminates the problem mentioned above that the error
reporting function must be called immediately after the function
causing the error since otherwise errno might have a different value.
error prints first the program name. If the application defined a
global variable error_print_progname and points it to a function this
function will be called to print the program name. Otherwise the
string from the global variable program_name is used. The program name
is followed by a colon and a space which in turn is followed by the
output produced by the format string. If the errnum parameter is
non-zero the format string output is followed by a colon and a space,
followed by the error message for the error code errnum. In any case
is the output terminated with a newline.
The output is directed to the stderr stream. If the stderr wasn’t
oriented before the call it will be narrow-oriented afterwards.
The function will return unless the status parameter has a non-zero
value. In this case the function will call exit with the status value
for its parameter and therefore never return. If error returns, the
global variable error_message_count is incremented by one to keep
track of the number of errors reported.
So maybe something like this would achieve OP's goal as well as the other answers suggested before:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <error.h>
int main(int argc, char* argv[])
{
if (argc == 2 && strcmp(argv[1], "-A") != 0) {
error(ENOENT, ENOENT, "cannot access %s", argv[1]);
}
printf("program didn't get to here\n");
}
Outputs of ls and this example:
~/workspace/tests/ $ ./ctest bogus_dir
./ctest: cannot access bogus_dir: No such file or directory
~/workspace/tests/ $ ls bogus_dir
ls: cannot access bogus_dir: No such file or directory

Sleep | warning implicit declaration of function `sleep'?

I am learning C.
In this program
I use sleep function to slowdown a count down.
My text book doesn't specify a library I should include to use the sleep function.
So I use it without including any special library for it and it works.
But it gives me this warning message in codeblocks.
I tried to include <windows.h> but still the same warning message appears.
warning D:\Project\C language\trial8\trial8.c|19|warning: implicit
declaration of function `sleep'|
And here is my code.
#include <stdio.h>
int main()
{
int start;
do
{
printf("Please enter the number to start\n");
printf("the countdown (1 to 100):");
scanf("%d",&start);
}
while(start<1 || start>100);
do
{
printf("T-minus %d\n",start);
start--;
sleep(3000);
}
while(start>0);
printf("Zero!\n Go!\n");
return(0);
}
I want to know what does the warning message mean? How important is it? Is there anything that I should do about it? Note that the program works anyway.
The issue is in the libraries (header files):
on Windows:
#include <windows.h> and Sleep(1000); => 1000 milliseconds
on Linux:
#include <unistd.h> and sleep(1); => 1 second
The function sleep is not part of C programming language. So, C compiler needs a declaration/prototype of it so that it can get to know about about number of arguments and their data types and return data type of the function. When it doesn't find it, it creates an Implicit Declaration of that function.
In Linux, sleep has a prototype in <unistd.h> and in windows, there is another function Sleep which has a prototype in <windows.h> or <synchapi.h>.
You can always get away with including header, if you explicitly supply the prototype of the function before using it. It is useful when you need only few functions from a header file.
The prototype of Sleep function in C on windows is:
VOID WINAPI Sleep(_In_ DWORD dwMilliseconds);
Remember, it is always a good practice to supply the prototype of the function being used either by including the appropriate header file or by explicitly writing it. Even, if you don't supply it, compiler will just throw a warning most of the time and it will make an assumption which in most cases will be something that you don't want. It is better to include the header file as API might change in future versions of the Library.
Windows doesn't have the sleep function. Instead, it has Sleep, which takes the number of milliseconds to sleep:
VOID WINAPI Sleep(
_In_ DWORD dwMilliseconds
);
You'll need to either #include <windows.h> or #include <synchapi.h>, depending on the version of Windows you're running. See MSDN for more details.
Update in 2022:
As it is stated on the Linux man page here we need to include unistd.h and should do fine for all OS.
#include <stdio.h>
#include <unistd.h>
int main()
{
sleep(1); /* sleep for 1 second*/
printf("END\n");
return 0;
}
To make it more cross-platform, try this:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

redirect input using open() and close()

I'm trying to understand how the I/O between processes mechanism works.
I have tried to run this code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
...
char *const paramList[] = {"/bin/cat"};
close(0);
open("./my_file", O_RDONLY);
execv("/bin/cat",paramList);
...
but got nothing going on. (it compiles, though)
what did I do wrong and what does my code do?
Per the docs for execv:
The array of pointers must be terminated by a NULL pointer.
Thus, add NULL to the end of paramList.
If you're still having trouble, make sure to check the return values from all syscalls, and see if any of your calls are failing.

Cannot compile with waitid() and P_PID

I am new to Linux. I am trying to use waitid() to wait for a child process.
When I try to compile a file including the following lines using gcc:
id_t cpid = fork();
siginfo_t status;
waitid(P_PID, cpid, &status, WEXITED);
The following error was generated:
error: ‘P_PID’ undeclared (first use in this function)
I included the following libraries:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
Did I miss something?
Another question is that how can I use WIFSIGNALED() to retrieve information from type siginfo_t?
You need to include <sys/wait.h> and define _XOPEN_SOURCE, as documented in the manual.
The WIFSIGNALED macro must be used with the integer status obtained from wait, waitpid or waitid. In the case of waitpid, the status is available as the si_status member of the siginfo_t structure. In other words, you would use WIFSIGNALED(info.si_status), info being a structure of type siginfo_t whose address you previously passed to waitid().

Resources