United States |
Previous | Contents | Index |
Waits for a child process to stop or terminate.
#include <wait.h>pid_t waitpid (pid_t process_id, int *status_location, int options);
process_id
The child process or set of child processes.status_location
A pointer to a location that contains the termination status of the child process as defined in the <wait.h> header file.Beginning with OpenVMS Version 7.2, when compiled with the _VMS_WAIT macro defined, this function puts the OpenVMS completion code of the child process at the address specified in the status_location argument.
options
Flags that modify the behavior of the function. These flags are defined in the Description section.
This function suspends the calling process until the request is completed. It is redefined so that only the calling thread is suspended.If the process_id argument is --1 and the options argument is 0, the waitpid function behaves the same as the wait function. If these arguments have other values, the waitpid function is changed as specified by those values.
The process_id argument allows the calling process to gather status from a specific set of child processes, according to the following rules:
If the process_id is Then status is requested Equal to --1 For any child process. In this respect, the waitpid function is equivalent to the wait function. Greater than 0 For a single child process and specifies the process ID. The waitpid function only returns the status of a child process from this set.
The options argument to the waitpid function modifies the behavior of the function. You can combine the flags for the options argument by specifying their bitwise-inclusive OR. The flags are:
WCONTINUED Specifies that the following is reported to the calling process: the status of any continued child process specified by the process_id argument whose status is unreported since it continued. WNOWAIT Specifies that the process whose status is returned in status_location is kept in a waitable state. You can wait for the process again with the same results. WNOHANG Prevents the calling process from being suspended. If there are child processes that stopped or terminated, one is chosen and waitpid returns its pid, as when you do not specify the WNOHANG flag. If there are no terminated processes (that is, if waitpid suspends the calling process without the WNOHANG flag), 0 (zero) is returned. Because you can never wait for process 0, there is no confusion arising from this return. WUNTRACED Specifies that the call return additional information when the child processes of the current process stop because the child process received a SIGTTIN, SIGTTOU, SIGSTOP, or SIGTSTOP signal. If the waitpid function returns because the status of a child process is available, the process ID of the child process is returned. Information is stored in the location pointed to by status_location, if this pointer is not null. The value stored in the location pointed to by status_location is 0 only if the status is returned from a terminated child process that did one of the following:
- Returned 0 from the main function.
- Passed 0 as the status argument to the _exit or exit function.
Regardless of the value of status_location, you can define this information using the macros defined in the <wait.h> header file, which evaluate to integral expressions. In the following function descriptions, status_value is equal to the integer value pointed to by status_location:
wifexited (status_value ) Evaluates to a nonzero value if status was returned for a child process that terminated normally. wexitstatus (status_value ) If the value of wifexited (status_value ) is nonzero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to the _exit or exit function, or to the value the child process returned from the main function. wifsignaled (status_value ) Evaluates to nonzero value if status returned for a child process that terminated due to the receipt of a signal not caught. wtermsig (status_value ) If the value of wifsignaled (status_value ) is nonzero, this macro evaluates to the number of the signal that caused the termination of the child process. wifstopped (status_value ) Evaluates to a nonzero value if status was returned for a child process that is currently stopped. wstopsig (status_value ) If the value of wifstopped (status_value ) is nonzero, this macro evaluates to the number of the signal that caused the child process to stop. wifcontinued (status_value ) Evaluates to a nonzero value if status returned for a child process that continued. If the information stored at the location pointed to by status_location is stored there by a call to waitpid that specified the wuntraced flag, one of the following macros evaluates to a nonzero value:
- wifexited (*status_value)
- wifsignaled (*status_value)
- wifstopped (*status_value)
- wifcontinued (*status_value)
If the information stored in the buffer pointed to by status_location resulted from a call to waitpid without the wuntraced flag specified, one of the following macros evaluates to a nonzero value:
- wifexited (*status_value)
- wifsignaled (*status_value)
If a parent process terminates without waiting for all of its child processes to terminate, the remaining child processes is assigned a parent process ID equal to the process ID of the init process.
See also exit , _exit , and wait in this section.
0 Indicates success. If the wnohang option was specified, and there are no stopped or exited child processes, the waitpid function also returns a value of 0. --1 Indicates an error; errno is set to one of the following values:
- ECHILD---The calling process has no existing unwaited-for child processes. The process or process group ID specified by the process_id argument does not exist or is not a child process of the calling process.
- EINTR---The function was terminated by receipt of a signal.
If the waitpid function returns because the status of a child process is available, the process ID of the child is returned to the calling process. If they return because a signal was caught by the calling process, --1 is returned.
- EFAULT--- The status_location argument points to a location outside of the address space of the process.
- EINVAL--- The value of the options argument is not valid.
Converts the wide character to its multibyte character representation.
#include <wchar.h>size_t wcrtomb (char *s, wchar_t wc, mbstate_t *ps);
s
A pointer to the resulting multibyte character.wc
A wide character.ps
A pointer to the mbstate_t object. If a NULL pointer is specified, the function uses its internal mbstate_t object.
If s is a NULL pointer, the wcrtomb function is equivalent to the call:
wcrtomb (buf, L'\0', ps)where buf is an internal buffer.
If s is not a NULL pointer, the wcrtomb function determines the number of bytes needed to represent the multibyte character that corresponds to the wide character specified by wc (including any shift sequences), and stores the resulting bytes in the array whose first element is pointed to by s. At most mb_cur_max bytes are stored.
If wc is a null wide character, a null byte is stored preceded by any shift sequence needed to restore the initial shift state. The resulting state described is the initial conversion state.
n The number of bytes stored in the resulting array, including any shift sequences to represent the multibyte character. --1 Indicates an encoding error. The wc argument is not a valid wide character. The global errno is set to eilseq ; the conversion state is undefined.
Concatenates two wide-character strings.
#include <wchar.h>Function Variants This function also has variants named _wcscat32 and _wcscat64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.wchar_t *wcscat (wchar_t *wstr_1, const wchar_t *wstr_2);
wstr_1, wstr_2
Pointers to null-terminated wide-character strings.
This function appends the wide-character string wstr_2, including the terminating null character, to the end of wstr_1. See also wcsncat in this section.
x The first argument, wstr_1, which is assumed to be large enough to hold the concatenated result.
#include <stdlib.h> #include <stdio.h> #include <wchar.h> #include <string.h> /* This program concatenates two wide-character strings using */ /* the wcscat function, and then manually compares the result */ /* to the expected result */ #define S1LENGTH 10 #define S2LENGTH 8 main() { int i; wchar_t s1buf[S1LENGTH + S2LENGTH]; wchar_t s2buf[S2LENGTH]; wchar_t test1[S1LENGTH + S2LENGTH]; /* Initialize the three wide-character strings */ if (mbstowcs(s1buf, "abcmnexyz", S1LENGTH) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } if (mbstowcs(s2buf, " orthis", S2LENGTH) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } if (mbstowcs(test1, "abcmnexyz orthis", S1LENGTH + S2LENGTH) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } /* Concatenate s1buf with s2buf, placing the result into */ /* s1buf. Then compare s1buf with the expected result */ /* in test1. */ wcscat(s1buf, s2buf); for (i = 0; i < S1LENGTH + S2LENGTH - 2; i++) { /* Check that each character is correct */ if (test1[i] != s1buf[i]) { printf("Error in wcscat\n"); exit(EXIT_FAILURE); } } printf("Concatenated string: <%S>\n", s1buf); }Running the example produces the following result:
Concatenated string: <abcmnexyz orthis>
Scans for a wide character in a specifed wide-character string.
#include <wchar.h>Function Variants This function also has variants named _wcschr32 and _wcschr64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.wchar_t *wcschr (const wchar_t *wstr, wchar_t wc);
wstr
A pointer to a null-terminated wide-character string.wc
A character of type wchar_t .
This function returns the address of the first occurrence of a specified wide character in a null-terminated wide-character string. The terminating null character is considered to be part of the string. See also wcsrchr in this section.
x The address of the first occurrence of the specified wide character. NULL Indicates that the wide character does not occur in the string.
#include <stdlib.h> #include <stdio.h> #include <wchar.h> #include <string.h> #define BUFF_SIZE 50 main() { int i; wchar_t s1buf[BUFF_SIZE]; wchar_t *status; /* Initialize the buffer */ if (mbstowcs(s1buf, "abcdefghijkl lkjihgfedcba", BUFF_SIZE) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } /* This program checks the wcschr function by incrementally */ /* going through a string that ascends to the middle and then */ /* descends towards the end. */ for (i = 0; (s1buf[i] != '\0') && (s1buf[i] != ' '); i++) { status = wcschr(s1buf, s1buf[i]); /* Check for pointer to leftmost character - test 1. */ if (status != &s1buf[i]) { printf("Error in wcschr\n"); exit(EXIT_FAILURE); } } printf("Program completed successfully\n"); }When this example program is run, it produces the following result:
Program completed successfully
Compares two wide-character strings. It returns an integer that indicates if the strings are different, and how they differ.
#include <wchar.h>int wcscmp (const wchar_t *wstr_1, const wchar_t *wstr_2);
wstr_1, wstr_2
Pointers to null-terminated wide-character strings.
This function compares the wide characters in wstr_1 with those in wstr_2. If the characters differ, the function returns:
- An integer less than 0, if the codepoint of the first differing character in wstr_1 is less than the codepoint of the corresponding character in wstr_2
- An integer greater than 0, if the codepoint of the first differing character in wstr_1 is greater than the codepoint of the corresponding character in wstr_2
If the wide-characters strings are identical, the function returns zero.
Unlike the wcscoll function, the wcscmp function compares the string based on the binary value of each wide character.
See also wcsncmp in this section.
< 0 Indicates that wstr_1 is less than wstr_2. = 0 Indicates that wstr_1 equals wstr_2. > 0 Indicates that wstr_1 is greater than wstr_2.
Compares two wide-character strings and returns an integer that indicates if the strings differ, and how they differ. The function uses the collating information in the LC_COLLATE category of the current locale to determine how the comparison is performed.
#include <wchar.h>int wcscoll (const wchar_t *ws1, const wchar_t *ws2);
ws1, ws2
Pointers to wide-character strings.
This function, unlike wcscmp , compares two strings in a locale-dependent manner. Because no value is reserved for error indication, the application must check for one by setting errno to 0 before the function call and testing it after the call.See also the wcsxfrm function in this section.
< 0 Indicates that ws1 is less than ws2. 0 Indicates that the strings are equal. > 0 Indicates that ws1 is greater than ws2.
Copies the wide-character string wstr_2, including the terminating null character, into wstr_1.
#include <wchar.h>Function Variants This function also has variants named _wcscpy32 and _wcscpy64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.wchar_t *wcscpy (wchar_t *wstr_1, const wchar_t *wstr_2);
wstr_1, wstr_2
Pointers to null-terminated wide-character strings.
This function copies wstr_2 into wstr_1, and stops after copying wstr_2's null character. If copying takes place between two ovelapping strings, the behavior is undefined.See also wcsncpy in this section.
x The address of wstr_1.
Compares the characters in a wide-character string against a set of wide characters. The function returns the length of the initial substring that is comprised entirely of characters that are not in the set of wide characters.
#include <wchar.h>size_t wcscspn (const wchar_t *wstr1, const wchar_t *wstr2);
wstr1
A pointer to a null-terminated wide-character string. If this is a null string, 0 is returned.wstr2
A pointer to a null-terminated wide-character string that contains the set of wide characters for which the function will search.
This function scans the wide characters in the string pointed to by wstr1 until it encounters a character found in wstr2. The function returns the length of the initial segment of wstr1 that is formed by characters not found in wstr2.
x The length of the segment.
#include <stdlib.h> #include <stdio.h> #include <wchar.h> #include <string.h> /* This test sets up 2 strings, buffer and w_string, and then */ /* uses wcscspn() to calculate the maximum segment of w_string, */ /* which consists entirely of characters NOT from buffer. */ #define BUFF_SIZE 20 #define STRING_SIZE 50 main() { wchar_t buffer[BUFF_SIZE]; wchar_t w_string[STRING_SIZE]; size_t result; /* Initialize the buffer */ if (mbstowcs(buffer, "abcdefg", BUFF_SIZE) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } /* Initialize the string */ if (mbstowcs(w_string, "jklmabcjklabcdehjklmno", STRING_SIZE) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } /* Using wcscspn - work out the largest string in w_string which */ /* consists entirely of characters NOT from buffer */ result = wcscspn(w_string, buffer); printf("Longest segment NOT found in w_string is: %d", result); }
Running the example program produces the following result:
Longest segment NOT found in w_string is: 4 |
Previous | Next | Contents | Index |
|