Skip to content

Instantly share code, notes, and snippets.

View rajkumar-p's full-sized avatar

Rajkumar rajkumar-p

View GitHub Profile
@rajkumar-p
rajkumar-p / pipe_sync.cpp
Created September 3, 2024 09:59
Using pipes to synchronize between parent & child
#include <cstring>
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[], char *envp[]) {
int p_fds[2];
if (pipe(p_fds) == -1) {
std::cerr << "Error: pipe() - " << strerror(errno) << std::endl;
exit(1);
@rajkumar-p
rajkumar-p / mimic_shell.cpp
Created April 3, 2024 14:47
A sample shell like program that runs: ls -lt | wc -l
#include <cstring>
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[], char *envp[]) {
int p_fds[2];
if (pipe(p_fds) == -1) {
std::cerr << "Error: pipe() - " << strerror(errno) << std::endl;
exit(1);
@rajkumar-p
rajkumar-p / pipe.cpp
Last active March 30, 2024 13:34
Linux: Parent process sending data to child, using a pipe
#include <cstring>
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#define BUF_SIZE 10
int main(int argc, char *argv[]) {
int p_fds[2];
if (pipe(p_fds) == -1) {
@rajkumar-p
rajkumar-p / exists_fine_grained.cpp
Created March 14, 2020 05:03
Check for an element in fine-grained list
template<typename T>
bool SortedList<T>::exists(T elem)
{
NodeListWithLock<T> *prev = this->_head;
prev->lock_node();
NodeListWithLock<T> *curr = prev->_next;
while (curr != nullptr &&
curr->data() < elem) {
curr->lock_node();
@rajkumar-p
rajkumar-p / remove_fine_grained.cpp
Created March 11, 2020 19:19
remove() using fine-grained strategy
template<typename T>
bool SortedList<T>::remove(T elem)
{
NodeListWithLock<T> *prev = this->_head;
prev->lock_node();
NodeListWithLock<T> *curr = prev->_next;
while (curr != nullptr &&
curr->data() < elem) {
curr->lock_node();
@rajkumar-p
rajkumar-p / add_fine_grained.cpp
Created March 11, 2020 19:01
add() using a fine-grained strategy
template<typename T>
bool SortedList<T>::add(T elem)
{
NodeListWithLock<T> *prev = this->_head;
prev->lock_node();
NodeListWithLock<T> *curr = prev->_next;
while(curr != nullptr &&
curr->data() < elem) {
curr->lock_node();
@rajkumar-p
rajkumar-p / sorted_list.hpp
Last active March 11, 2020 19:04
SortedList Interface
template<typename T>
class SortedList : public Set<T>
{
private:
NodeListWithLock<T> *_head;
public:
SortedList();
~SortedList();
@rajkumar-p
rajkumar-p / node_list_with_lock.cpp
Last active March 11, 2020 18:44
NodeListWithLock Class
template<typename T>
class NodeListWithLock
{
private:
T _data;
std::mutex _mux;
public:
NodeListWithLock<T> _next;
@rajkumar-p
rajkumar-p / remove_element.cpp
Created March 2, 2020 18:11
Remove an element from the concurrent list
template<typename T>
bool List<T>::remove_if_exists(T elem)
{
// Empty list
if (this->empty()) {
return false;
}
// Need to remove the head element
if (this->_head->data() == elem) {
@rajkumar-p
rajkumar-p / file_object.cpp
Last active February 23, 2020 07:59
File Object that encapsulates FILE *
class FileObject {
private:
FILE *_fp;
public:
FileObject(const std::string &file_name, const std::string &mode);
~FileObject();
// Copy/Assignment according to your usecase